java继承的练习

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43824157/article/details/87555984

大一下学期开始,想记录一下自己的学习过程,同时也为了激励自己。
前几天看完java继承的视频,感觉容易,没想到到后面的课程学习起来越来越吃力,就回来做了一些简单的关于继承的题目。
0.按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。

public class JuXing {
	private int longs;
	private int wides;
	public JuXing(int longs, int wides) {
		super();
		this.longs = longs;
		this.wides = wides;
	}
//	int getLongs() {
//		return longs;
//	}
//	int getWides() {
//		return wides;
//	}
	void print() {
		System.out.print("长 "+longs+"宽 "+wides);
	}
	int upArea() {
		return longs*wides;
	}
}
public class ChangFangTi extends JuXing {
	private int height;
	public ChangFangTi(int longs, int wides, int height) {
		super(longs, wides);
		this.height = height;
	}
	void print() {
		super.print();
		System.out.println("高 "+height);
	}
	void volume() {
		System.out.println(upArea());
		System.out.println(upArea()*height);
	}

}
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JuXing jx = new JuXing(20, 30);
		ChangFangTi cft = new ChangFangTi(20, 30, 40);
		cft.volume();
	}

}

1.创建People(父类),ChinaPeople,AmericanPeople三个类并添加一些内容。

public class People {
	private double height;
	private double weight;
	public double getHeight() {
		return height;
	}
	public double getWeight() {
		return weight;
	}
	
	public People(double height, double weight) {
		super();
		this.height = height;
		this.weight = weight;
	}
	public void speakHello() {
		System.out.println("Hello");
	}
	public void averageHeight() {
		System.out.println("The people's average Height is "+height);
	}
	public void averageWeight() {
		System.out.println("The people's aversht weight is "+weight);
	}
}
public class ChinaPeople extends People {
	public ChinaPeople(double height, double weight) {
		super(height, weight);
		// TODO Auto-generated constructor stub
	}
	public void speakHello() {
		System.out.println("你好");
	}
	public void averageHeight() {
		System.out.println("中国人的平均身高是"+getHeight());
	}
	public void averageWeight() {
		System.out.println("中国人的平均体重是"+getWeight());
	}
	public void chinaKungFu() {
		System.out.println("我会中国武术");
	}
}
public class AmericanPeople extends People{

	public AmericanPeople(double height, double weight) {
		super(height, weight);
		// TODO Auto-generated constructor stub
	}
	public void averageHeight() {
		System.out.println("The American's average height is "+ getHeight());
	}
	public void averageWeight() {
		System.out.println("The American's average weight is "+getWeight());
	}
	public void AmericanQ() {
		System.out.println("I can use Q");
	}
}
public class TestPeople {
	void Test( People p) {
		p.speakHello();
		p.averageHeight();
		p.averageWeight();
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		People people = new People(170, 60);
		ChinaPeople chinese = new ChinaPeople(175, 59);
		AmericanPeople american = new AmericanPeople(180, 70);
//		people.averageHeight();
//		people.averageWeight();
//		chinese.averageHeight();
//		chinese.averageWeight();
//		american.averageHeight();
//		american.averageWeight();
		TestPeople tp = new TestPeople();
		tp.Test(people);
		tp.Test(chinese);
		chinese.chinaKungFu();
		tp.Test(american);
		american.AmericanQ();
	}

}

构造方法是来初始化实例变量的,子类构造方法初始化之前会首先初始化父类,在子类中要通过super来调用父类的构造方法。
方法重写可以改变父类的方法,方法名称、变量必须与父类的相同。
函数必须通过对象来调用。

展开阅读全文

没有更多推荐了,返回首页