类的封装,继承与多态(Java8.6实战练习)

1.建立一个人类(Person)和学生类(Student),功能要求如下:

  • a. Person中包含4个数据成员name, addr, gender, age,分别表示姓名、地址、性别和年龄。设计一个输出方法talk()来显示这4种属性。
  • b. Student类继承Person类,并增加成员Math、English存放数学与英语成绩。用1个六参构造方法、1个两参构造方法、1个无参构造方法和覆写输出方法talk()用于显示6种属性。对于构造方法参数个数不足以初始化4个数据成员时,在构造方法中采用自己指定默认值来实施初始化。

代码如下:(纯手打)

class Person
{
	String name;
	String addr;
	String gender;
	int age;
	public Person(String name,String addr,String gender,int age)  //构造函数
	{
		this.name = name;
		this.addr = addr;
		this.gender = gender;
		this.age = age;
	}
	public String talk()
	{
		return "I am " + name + ".I live in " + addr + ".And I am " + age + " years old, a " + gender + ".";
	}
	
}
class Student extends Person
{
	int Math;    //表示Math分数
	int English;
	public Student(String name,String addr,String gender,int age,int Math,int English)  //六参构造函数
	{
		super(name, addr,gender,age);
		this.Math = Math;
		this.English = English;
	}
	public Student (int Math,int English)      //构造方法重载1:两参构造函数
	{
		super("Mike", "America", "boy",20);     //先实例化父类对象
		this.Math = Math;
		this.English = English;
	}
	public Student ()      //构造方法重载2:无参构造函数
	{
		super("Mike", "America", "boy",20);
		Math = 90;
		English = 90;
	}
	public String talk()
	{
		return super.talk()+"The Math score is " + Math + " and the English score is " + English + ".";
	}
}
public class Test_8_6_1 
{
	public static void main(String[] args) 
	{
		Person p = new Person("Jonh", "America", "boy", 21);    //实例化父类对象
		Student s1 = new Student("Joshua", "China", "boy", 30, 90, 90);  //6实参
		Student s2 = new Student(91, 91);  //2实参
		Student s3 = new Student();  //none实参
		System.out.println(p.talk());
		System.out.println(s1.talk());
		System.out.println(s2.talk());
		System.out.println(s3.talk());
	}
}

运行结果如下:

I am Jonh.I live in America.And I am 21 years old, a boy.
I am Joshua.I live in China.And I am 30 years old, a boy.The Math score is 90 and the English score is 90.
I am Mike.I live in America.And I am 20 years old, a boy.The Math score is 91 and the English score is 91.
I am Mike.I live in America.And I am 20 years old, a boy.The Math score is 90 and the English score is 90.

2.定义一个instrument(乐器)类,并定义其公有方法play(),再分别定义其子类Wind(管乐器),Percussion(打击乐器),Stringed(弦乐器),覆写play方法,实现每种乐器独有的play方式。最后在测试类中使用多态的方法执行每个子类的paly()方法。
这题比较简单,主要考察对象多态性,代码:

class Instrument
{
	public void play()
	{
		System.out.println("Choose one instrument and paly!");
	}
}
class Wind extends Instrument
{
	public void play()
	{
		System.out.println("I want to play Wind!");
	}
}
class Percussion extends Instrument
{
	public void play()
	{
		System.out.println("I want to play Percussion!");
	}
}
class Stringed extends Instrument
{
	public void play()
	{
		System.out.println("I want to play Stringed!");
	}
}
public class Test_8_6_2 
{
	public static void main(String[] args) 
	{
		Instrument i;     //不实例化父类对象
		Wind w = new Wind();
		Percussion p = new Percussion();
		Stringed s = new Stringed();
		i = w;   i.play();
		i = p;   i.play();
		i = s;   i.play();   //向上转型
	}
}

运行结果:

I want to play Wind!
I want to play Percussion!
I want to play Stringed!

你可能感兴趣的:(Java)