java用接口、多態(tài)、繼承、類計(jì)算三角形和矩形周長及面積的方法
更新時(shí)間:2015年05月19日 11:44:09 作者:一羽清寧
這篇文章主要介紹了java用接口、多態(tài)、繼承、類計(jì)算三角形和矩形周長及面積的方法,涉及java面向?qū)ο笾蓄?、接口、多態(tài)等的使用技巧,需要的朋友可以參考下
本文實(shí)例講述了java用接口、多態(tài)、繼承、類計(jì)算三角形和矩形周長及面積的方法。分享給大家供大家參考。具體如下:
定義接口規(guī)范:
/**
* @author vvv
* @date 2013-8-10 上午08:56:48
*/
package com.duotai;
/**
*
*
*/
public interface Shape {
public double area();
public double longer();
}
/**
* @author vvv
* @date 2013-8-10 上午09:10:06
*/
package com.duotai;
/**
*
*
*/
public class Triangle implements Shape {
double s1;
double s2;
double s3;
// 初始化一個(gè)三角形對象,并賦予該三角形三邊長
public Triangle(double s1, double s2, double s3) {
if (isTri(s1, s2, s3)) {
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
} else {
System.out.println("輸入的三邊長" + s1 + "、" + s2 + "、" + s3
+ "不能組成一個(gè)三角形,請重新輸入三邊長!");
}
}
// 判斷是否是個(gè)三角形
public boolean isTri(double s1, double s2, double s3) {
if (s1 + s2 < s3) {
return false;
}
if (s1 + s3 < s2) {
return false;
}
if (s2 + s3 < s1) {
return false;
}
return true;
}
/*
* (non-Javadoc)
*
* @see com.duotai.Shape#area()
*/
@Override
public double area() {
double p = (s1 + s2 + s3) / 2;
return Math.sqrt(p * (p - s1) * (p - s2) * (p - s3));
}
/*
* (non-Javadoc)
*
* @see com.duotai.Shape#longer()
*/
@Override
public double longer() {
return s1 + s2 + s3;
}
}
/**
* @author vvv
* @date 2013-8-10 上午09:12:06
*/
package com.duotai;
/**
*
*
*/
public class Director implements Shape {
double s1;
double s2;
// 初始化一個(gè)長方形,并賦予該長方形兩邊長
public Director(double s1, double s2) {
this.s1 = s1;
this.s2 = s2;
}
/*
* (non-Javadoc)
*
* @see com.duotai.Shape#area()
*/
@Override
public double area() {
// TODO Auto-generated method stub
return s1 * s2;
}
/*
* (non-Javadoc)
*
* @see com.duotai.Shape#longer()
*/
@Override
public double longer() {
// TODO Auto-generated method stub
return 2 * (s1 + s2);
}
}
/**
* @author vvv
* @date 2013-8-10 上午09:13:30
*/
package com.duotai;
/**
*
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Shape triangle = new Triangle(3, 4, 8);
// 新建一個(gè)三邊長為3,4,5的三角形
Shape tri = new Triangle(3, 4, 5);
Shape director = new Director(10, 20);
// 新建一個(gè)兩邊長為10,20的長方形
System.out.println("三角形triangle的周長為:" + triangle.longer());
System.out.println("三角形triangle的面積為:" + triangle.area());
System.out.println("三角形tri的周長為:" + tri.longer());
System.out.println("三角形tri的面積為:" + tri.area());
System.out.println("該長方形的周長為:" + director.longer());
System.out.println("該長方形的面積為:" + director.area());
}
}
希望本文所述對大家的java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring的@ConfigurationProperties注解詳解
這篇文章主要介紹了Spring的@ConfigurationProperties注解詳解,@ConfigurationProperties該注解是用來獲取yml或者properties配置文件的配置信息,下面根據(jù)一些配置信息給出案例代碼進(jìn)行講解,需要的朋友可以參考下2023-11-11
Spring中的EurekaServer啟動(dòng)詳解
這篇文章主要介紹了Spring中的EurekaServer啟動(dòng)詳解,初始化eureka,包含eureka集群的同步和發(fā)布注冊,這個(gè)方法時(shí)重寫ServletContextListener#contextInitialized,是eureka啟動(dòng)的入口了,需要的朋友可以參考下2023-11-11
Java實(shí)用小技能之快速創(chuàng)建List常用幾種方式
java集合可以說無論是面試、刷題還是工作中都是非常常用的,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)用小技能之快速創(chuàng)建List常用的幾種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Java面試之動(dòng)態(tài)規(guī)劃與組合數(shù)
這篇文章主要介紹了Java面試之動(dòng)態(tài)規(guī)劃與組合數(shù)的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09

