The use of more than one member state
1.1 Features:
- Member variables: using the parent class
- Member method: Due to rewrite phenomenon so using a subclass
- Static member: With the object exists, who called who returns
public class Test1_UseManyTai {
public static void main(String[] args) {
// 创建多态对象测试
Fu f = new Zi();// 口诀1 :父类引用 指向 子类对象
System.out.println(f.count); //1、成员变量用父类的!!!口诀2: 编译看左边,运行看右边(针对重写现象)。
f.eat(); // 口诀2: 编译看左边,运行看右边(针对重写现象)。
//2、 编译看左边,eat()调用的是父类的方法声明,但是方法体使用的是子类的。
// 编译看左边更好的体现了多态可以用来 统一调用标准,就是都向 父类 看齐,必须使用父类提供了的功能。
//3、静态方法能重写吗???---静态资源不能重写
f.sleep();//使用的仍然是父类的功能
}
}
// 创建父类
class Fu {
int count = 10;
public void eat() {
System.out.println("Fu..eat()...");
}
// 静态方法
static public void sleep() {
System.out.println("Fu..sleep()...");
}
}
// 创建子类
class Zi extends Fu {
int count = 20;
public void eat() {
System.out.println("Zi..eat()...");
}
// 静态方法 -- 真的是重写现象吗---不存在!!
static public void sleep() {
System.out.println("Zi..sleep()...");
}
}
2 Permissions modifier
- Used to control a class, range or access class members.
3 abnormal
3.1 Overview:
- Used to encapsulate target error messages.
- Makeup: Types, Tips, line number.
3.2 abnormal inheritance structure:
- Throwable - top-level parent class
- - Error: System Error, can not be repaired
- - Exception: correctable error
- –RunTimeException
- –ClassCastException
- –ClassNotFoundException
3.3 Exception Handling:
- Program encountered an exception, there are usually two approaches: the capture or throw up
- When you call a method throws an exception, call the location can not continue to throw up process can also catch the exception.
4 abstract class
4.1 concept:
- Java can be defined method is not a method thereof, by which a subclass to a specific implementation.
- This method is not a method we call the body abstract methods abstract class containing method we call an abstract class.
- An abstract class can be understood as a special class method declaration is not only a method body.
//把共性的代码,向上提取,形成父类 -- 形成继承的结构好处是可以复用父类的代码
//抽象类是个特殊的类,特殊在哪儿?特殊在类里可以有普通方法,还可以有抽象方法。
抽象的 class Animal{
//父类的方法体让子类重写了!!!--这时父类的方法体有点多余!!!父类干脆就不提供方法体--抽象方法
抽象的 public void eat() ;
public void sleep(){
syso(....);
}
}
class Dog extends Animal{
public void eat(){//重写目的是修改父类原有的功能
syso(.);
}
}
4.2 Features:
- Java achieved by keyword abstract
- The method may be modified or class
- An abstract class can be no abstract methods (to be implemented by subclasses)?
- If the abstract methods in the class, that class must be defined as an abstract class
- Subclass inherits the abstract class in the future, or is an abstract class, or put all the abstract methods are rewritten
- Used for multiple states
- An abstract class can not be instantiated new
Usage abstract class 5
public class Test4_UseAbstract {
public static void main(String[] args) {
//创建多态对象测试
Father f = new Son();
//Father f2 = new Father();
}
}
//创建父类
abstract class Father{
//1、抽象类能创建对象吗?--不能 !!
//2、那么抽象类里的构造方法有啥用??---方便子类创建对象!!
public Father() {
System.out.println("Father()...");
}
}
//创建子类
class Son extends Father{
public Son() {
super();//默认就存在
System.out.println("Son()...");
}
}