Java面向对象练习题继承之人种

Posted 木头鸽子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java面向对象练习题继承之人种相关的知识,希望对你有一定的参考价值。

 

//People类

package 继承;

public class People {
	
	private String name;
	private double height;
	private double weight;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getHeight() {
		return height;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	public double getWeight() {
		return weight;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
	
	public void speakHello()
	{
		System.out.println("Hello");
	}

}

  

 

//Chinapeople类

package 继承;


//继承
//Object 是所有类的父类
public class Chinapeople extends People {

	public void Chinesekongfu()
	{
		System.out.println("坐如钟,站如松");
	}
	
	//重写 也叫覆盖
	public void speakHello()
	{
		System.out.println("你好");
	}
}

  

//Americanpeople类

package 继承;

public class Americanpeople extends People {
	
	public void americanBoxing()
	{
		System.out.println("直拳,勾拳");
	}
}

  

 

//Testpeople类

package 继承;

public class Textpeople {

	public static void main(String[] args) {
		
		People p=new People();
		p.speakHello();
		
		System.out.println("中国人:");
		Chinapeople cp=new Chinapeople();
		cp.speakHello();
		cp.Chinesekongfu();
		
		System.out.println("美国人:");
		Americanpeople ap=new Americanpeople();
		ap.speakHello();
		ap.americanBoxing();
	}

}

  

 

运行结果

以上是关于Java面向对象练习题继承之人种的主要内容,如果未能解决你的问题,请参考以下文章

Java面向对象练习题继承之物种

面向对象之继承 练习题

Java入门之面向对象-划重点

面向对象之继承

面向对象之继承与派生

Java面向对象编程之继承