package cn.csdn.abstracts;
//这个类用来测试抽象类的用法及其特点
public class Test3_Abstract {
public static void main(String[] args) {
//创建子类对象,测试使用类
//The value of the local variable a/a2/a3 is not used
Advance a = new Advance();
a.eat();
a.sleep();
// //创建父类对象测试,普通父类
// Advances a2 = new Advances();
// a2.eat();
// a2.sleep();
//创建多态对象测试
//编译看左边,执行看右边:调用父类里的成员,执行子类里经过覆写的方法体
//创建父类对象,通过实例化子类对象的向上转型,抽象类/特殊类
Advances a3 = new Advance();
a3.eat();
a3.sleep();
/**Advance.eat();
Cannot make a static reference to the non-static method eat() from the type Advance
从这句话你能看出来什么,普通类,或者只要它是一个类,那以这个类调用的方式,是静态的。或者类本身,不管它是什么类,是静态的。
static修饰,随类的加载而加载而存在,然而,非static修饰,需先通过创建实例化对象,才能分配到内存空间,才能存在,才能被
实质性地以这个内存空间(其地址值)进行调用。*/
}
}
//把共性功能向上提取,形成父类
abstract class Advances{
/** 先不做程序设计: 抽象类 == 程序设计(的结果) */
//抽象类的演化过程:
abstract public void eat();
public abstract void sleep();
}
//创建一个普通类,关键字class,强制继承父类,拥有父类的所有功能,耦合性太强,关键字extends
class Advance extends Advances{
//当需要修改父类的原有功能,发生重写
public void eat() {
System.out.println("自己的生活,只有自己知道");
}
public void sleep() {
System.out.println("即使睡不着,也淡然着,因为我的身体告诉我此时不需要睡眠");
}
}
//创建一个普通类,关键字class
class Advance2{
/** 先不做程序设计: 抽象类 == 程序设计(的结果) */
public void eat() {
System.out.println("每天都要吃得好好的,饱饱的");
}
public void sleep() {
System.out.println("每天都要早睡早起,精神才好,体力才能充沛");
}
}
//创建一个普通类,关键字class
class Advance3{
/** 先不做程序设计: 抽象类 == 程序设计(的结果) */
public void eat() {
System.out.println("每天都要吃得好好的");
}
public void sleep() {
System.out.println("每天都要早睡早起");
}
}