5、定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性. 属性(成员变量)需要私有并提供get, set方法, 可以通过构造函数进行初始化.
package student;
/**
* @author 小媛
* Student class
* ① properties:name and age
* ②constructor , set() get() method
*/
public class Student {
private String name;
private int age;
Student(String name, int age){
this.name = name;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
6、编写一个类Person,为Person类定义年龄、姓名两个属性,并且定义一个SayHello方法,方法执行时输出“我是***我的年龄是***”;定义一个Chinese类从Person类继承。
package person;
/**
* @author 小媛
* Person class:
* ①age;name;
* ②sayHello method;
* Chinese class extends Person class;
*/
class Person {
private String name;
private int age;
Person(String name, int age){
this.name = name;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void sayHello(){
System.out.println("我是" + this.name + "我的年龄是:" + this.age );
}
}
package person;
public class Chinese extends Person{
Chinese(String name, int age){
super(name, age);
}
} |