抽象函数抽象类学习

abstract class Person{
	String name;
	int age;
	
	Person(){
		System.out.println("Person的构造函数");
	}
	
	Person(String name,int age){
		this.name = name;
		this.age = age;
	}
	
	void introduce(){
		System.out.println("我的名字是" + name + "我的年龄是" + age);
	}
	
	abstract void eat();
}

class Student extends Person{
	String address;
	Student(){
		//super();编译器默认添加该代码。
		System.out.println("Student的构造函数");
	}
	Student(String name,int age,String address){
		super(name,age);
		this.address = address;
	}
	void eat(){
		System.out.println(name + "同学下课去" + address + "吃饭啦"); 
	}	
}

class Test{
	public static void main(String[] args){
		Person p = new Student();
		p.name = "James";
		p.eat();
	}
}

抽象类不能够生成对象。
如果一个类中包含抽象函数,那么这个类必须声明成抽象类。
抽象类当中可以没有抽象函数。

猜你喜欢

转载自317324406.iteye.com/blog/2240594