javaLearn---re2继承多态练习

1.题目
(1)定义Point(点)、Circle(圆形)、Square(正方形)类。点信息包括x,y坐标。圆信息包括圆心坐标和半径。正方形信息包括中心坐标和边长。用关联(Point对象作为Circle和Square的成员)和继承(Circle和Square分别继承Point)两种方法实现并测试。
(2)代码实现


public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
     Circle1 c1=new Circle1(1,2,3);
     Circle2 c2=new Circle2(1,2,3);
     Square1 s1=new Square1(1,2,3);
     Square2 s2=new Square2(1,2,3);
     
	}

}
class Point{
	double x,y;

	public Point(double x, double y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	
}
//用关联(Point对象作为Circle和Square的成员)
class Circle2{
	Point point;
	double r;
	public Circle2(double x,double y, double r) {
		//用关联(Point对象作为Circle和Square的成员)
		this.point=new Point(x,y);
		this.r = r;
	}
	
}
//用关联(Point对象作为Circle和Square的成员)
class Square2{
	Point point;
	double a;
	public Square2(double x,double y, double a) {
		
		this.point = new Point(x,y);//记住
		this.a = a;
	}
	
}
//继承(Circle和Square分别继承Point)
class Circle1 extends Point{
	double r;

	public Circle1(double x, double y, double r) {
		super(x, y);
		this.r = r;
	}
	
}
//继承(Circle和Square分别继承Point)
class Square1 extends Point{
	double a;

	public Square1(double x, double y, double a) {
		super(x, y);
		this.a = a;
	}
	
}

(1)题目
定义抽象类Shape(形状),包含形状名字属性及相应构造方法。
定义两个接口,一个接口IArea定义求面积的方法,一个接口IVolume定义求体积的方法。
定义Shape类的子类矩形(Rectangle)类,实现IArea接口。
定义长方体类同时实现IArea(求表面积)和IVolume接口。
(2)代码实现
法一:


public class Main95 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
     Rectangle rec=new Rectangle("长方形",4,6);
     Cuboid cub=new Cuboid(4,5,6);
     System.out.println("长方形的面积:"+rec.area());
     System.out.println("长方体的面积:"+cub.area());
     System.out.println("长方体的体积:"+cub.volume());
     
     
	}

}
abstract class Shape{
	String name;

	public Shape(String name) {
		super();
		this.name = name;
	}	
}
interface Iarea{//接口 不是类 开头字母要小写
	double area();//定义成员函数
}
interface Ivolume{
	double volume();
}
 class Rectangle extends Shape implements Iarea{
	int len,wid;

	public Rectangle(String name, int len, int wid) {
		super(name);
		this.len = len;
		this.wid = wid;
	}

	@Override
	public double area() {
		// TODO Auto-generated method stub
		return this.len*this.wid;
	}
	
	
}
 class Cuboid  implements Iarea,Ivolume{
	 int len,wid,hig;

	public Cuboid( int len, int wid, int hig) {
		super();
		this.len = len;
		this.wid = wid;
		this.hig = hig;
	}

	@Override
	public double volume() {
		// TODO Auto-generated method stub
		return this.wid*this.len*this.hig;
	}

	@Override
	public double area() {
		// TODO Auto-generated method stub
		return 2*(len*wid*hig);
	}
    
	
}
/*长方形的面积:24.0
长方体的面积:240.0
长方体的体积:120.0*/

法2:


public class Main95 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
     Rectangle rec=new Rectangle("长方形",4,6);
     Cuboid cub=new Cuboid("长方体",4,5,6);
     System.out.println("长方形的面积:"+rec.area());
     System.out.println("长方体的面积:"+cub.area());
     System.out.println("长方体的体积:"+cub.volume());
     
     
	}

}
abstract class Shape{
	String name;

	public Shape(String name) {
		super();
		this.name = name;
	}	
}
interface Iarea{//接口 不是类 开头字母要小写
	double area();//定义成员函数
}
interface Ivolume{
	double volume();
}
 class Rectangle extends Shape implements Iarea{
	int len,wid;

	public Rectangle(String name, int len, int wid) {
		super(name);
		this.len = len;
		this.wid = wid;
	}

	@Override
	public double area() {
		// TODO Auto-generated method stub
		return this.len*this.wid;
	}
	
	
}
 class Cuboid extends Shape implements Iarea,Ivolume{
	 int len,wid,hig;

	public Cuboid(String name, int len, int wid, int hig) {
		super(name);
		this.len = len;
		this.wid = wid;
		this.hig = hig;
	}

	@Override
	public double volume() {
		// TODO Auto-generated method stub
		return this.wid*this.len*this.hig;
	}

	@Override
	public double area() {
		// TODO Auto-generated method stub
		return 2*(len*wid*hig);
	}
    
	
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值