Java 抽象类的应用

package hello;

abstract class Person{
	private int age;    // 声明公有的属性,年龄(age)和姓名(string)
	private String name;
	public int getAge() {
		return age;
	}
	public Person(int age, String name){  // 构造方法
		this.age = age;
		this.name = name;
	}
	public void setAge(int age) {  // 私有属性的 getattr 和 setattr 方法
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	abstract void want();  // 声明抽象函数
}

class Student extends Person{

     /*
     * Student 扩展了抽象类,并且抽象类中有构造方法和抽象方法,因此在 Student 类中要复写抽象类中的构造方法和抽象方法
    */ private int score; public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Student(int age, String name, int score) { super(age, name); this.score = score; } @Override void want() { System.out.println("Name: " + this.getName() + " Age: "+ this.getAge() + "Score :" + this.getScore()); } } public class Demo03 { public static void main(String[] args) { Student student = new Student(10, "zhangsan", 100); student.want(); } }

  

posted @ 2017-05-03 16:57  苌来看看  阅读(171)  评论(0编辑  收藏  举报