AbstractTest.java
package com.klvchen.java;
public class AbstractTest {
public static void main(String[] args) {
//一旦 Person 类抽象了,就不可以实例化
// Person p1 = new Person();
// p1.eat();
Person s1 = new Student("klvchen", 25);
s1.eat();
s1.walk();
}
}
abstract class Creature{
public abstract void breath();
}
abstract class Person extends Creature{
String name;
int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//不是抽象方法:
// public void eat() {
// System.out.println("人吃饭");
// }
//抽象方法
public abstract void eat();
public void walk() {
System.out.println("人走路");
}
}
class Student extends Person{
public Student(String name, int age) {
super(name, age);
}
public Student() {
}
public void eat() {
System.out.println("学生多吃有营养的食物");
}
@Override
public void breath() {
System.out.println("学生应该呼吸新鲜的没有雾霾的空气");
}
}
PersonTest.java
package com.klvchen.java;
public class PersonTest {
public static void main(String[] args) {
method(new Student()); //匿名对象
Worker worker = new Worker();
method1(worker); //非匿名的类非匿名的对象
method1(new Worker()); //非匿名的类匿名的对象
System.out.println("**********************");
// 创建了一个匿名子类的对象:p
Person p = new Person() {
@Override
public void eat() {
System.out.println("吃东西");
}
@Override
public void breath() {
System.out.println("好好呼吸");
}
};
method1(p);
System.out.println("****************************");
//创建匿名子类的匿名对象
method1(new Person() {
@Override
public void eat() {
System.out.println("吃好吃东西");
}
@Override
public void breath() {
System.out.println("好好呼吸新鲜空气");
}
});
}
public static void method1(Person p) {
p.eat();
p.breath();
}
public static void method(Student s) {
}
}
class Worker extends Person{
@Override
public void eat() {
// TODO Auto-generated method stub
}
@Override
public void breath() {
// TODO Auto-generated method stub
}
}