继承(2)

设计一个类Shape(图形)包含求面积和周长的area()方法和perimeter()方法以及设置颜色的方法SetColor(),并利用Java多态技术设计其子类Circle(圆形)类、Rectangle(矩形)类和Triangle(三角形)类,并分别实现相应的求面积和求周长的方法。每个类都要覆盖toString方法。

海伦公式:三角形的面积等于s(s-a)(s-b)(s-c)的开方,其中s=(a+b+c)/2

测试类中测试上面的类及其相应功能

要求(1)能够体现向上转型,向下转型等,Shape s1=new Circle();

体现this、super等关键字的两种使用及各子类/超类的构造方法的调用顺序;

package javaTest;

import java.math.*;

public class Demo05 {
public static void main(String[] args) {
	Shape s1=new Cir(5);
	s1.SetColor("蓝色");
	System.out.println(s1.toString());
	Shape s2=new Rectangle(2, 5);
	s2.SetColor("黄色");
	System.out.println(s2.toString());
	Shape s3=new Triangle(2, 2, 2);
	s3.SetColor("红色");
	System.out.println(s3.toString());
}
}
class Shape{
	String color;
	public String SetColor(String c){
		color=c;
		return color;
	}
    public double area(){
		return 0;
	}
    public double perimeter(){
		return 0;
	}
}
class Cir extends Shape{
    int r;
    public Cir(int r) {
		super();
		this.r = r;
	}

	final double pi=3.14;
	@Override
	public double area() {
		return pi*r*r;
	}

	@Override
	public double perimeter() {
		// TODO Auto-generated method stub
		return 2*pi*r;
	}

	@Override
	public String toString() {
		return "Cir area=" + area() + ", perimeter=" + perimeter() + "color="+super.color;
	}
	
}
class Rectangle extends Shape{
	int a;
	int b;
	public Rectangle(int a, int b) {
		super();
		this.a = a;
		this.b = b;
	}

	@Override
	public double area() {
		// TODO Auto-generated method stub
		return a*b;
	}

	@Override
	public double perimeter() {
		// TODO Auto-generated method stub
		return 2*(a+b);
	}

	@Override
	public String toString() {
		return "Rectangle area=" + area() + ", perimeter=" + perimeter() + ",color="+super.color;
	}
	
}
class Triangle extends Shape{
	int a;
	int b;
	int c;
	public Triangle(int a, int b, int c) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
	}
	@Override
	public double area() {
		double s=(a+b+c)/2;
		return Math.sqrt(s*(s-a)*(s-b)*(s-c));
	}
	@Override
	public double perimeter() {
		// TODO Auto-generated method stub
		return a+b+c;
	}
	@Override
	public String toString() {
		return "Triangle area=" + area() + ", perimeter=" + perimeter() + ",color:"+super.color;
	}
	
}

 

转载于:https://my.oschina.net/saysuqi/blog/807932

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
原型链继承(Prototype Inheritance)在JavaScript中是通过创建一个新对象并让它引用另一个对象的原型来实现的。例如: ```javascript function Parent() {} Parent.prototype.method = function() { console.log('Parent method'); }; let child = new Parent(); child.method(); // 输出: "Parent method" ``` **借用构造函数继承**(Constructo r Chaining)利用已有构造函数作为父,通过`new`关键字传递给子实例化过程,间接实现了继承: ```javascript function Parent() { this.parentProp = 'parent'; } function Child() { Parent.call(this); // 借用父构造函数 this.childProp = 'child'; } Child.prototype = Object.create(Parent.prototype); Child.prototype.constructor = Child; let childInstance = new Child(); console.log(childInstance.parentProp); // 输出: "parent" console.log(childInstance.childProp); // 输出: "child" ``` **组合式继承**(Mix-in or Prototype Mixing)结合原型链和构造函数继承,允许从多个源继承属性和方法: ```javascript function Mixin(target) { for (let prop in Mixin.prototype) { target[prop] = Mixin.prototype[prop]; } } function Parent() { this.parentProp = 'parent'; } Mixin(Parent.prototype); let child = new Parent(); console.log(child.parentProp); // 输出: "parent" ``` **ES6的class继承**(Class-based Inheritance)使用`extends`关键字实现: ```javascript class Parent { constructor() { this.parentProp = 'parent'; } parentMethod() { console.log('Parent method'); } } class Child extends Parent { constructor() { super(); this.childProp = 'child'; } childMethod() { console.log('Child method'); } } let childInstance = new Child(); childInstance.parentMethod(); // 输出: "Parent method" childInstance.childMethod(); // 输出: "Child method" ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值