mysql 檢視 索引_Mysql 高階
阿新 • • 發佈:2020-12-17
package com.oop; import com.oop.demo06.Person; import com.oop.demo06.Student; public class Application { public static void main(String[] args) { //一個物件的實際型別是確定的 //new Person(); //new Student(); //可以指向的引用型別就不確定了 : 父類的引用指向子類 //Student 能呼叫的方法都是自己或者繼承父類的! Student s1 = new Student(); //Person 父型別,可以指向子類,但不能呼叫子類獨有的方法! Person s2 = new Student(); Object s3 = new Student(); s2.run();//子類重寫了父類的方法,執行子類的方法 s1.run(); //物件能執行那些方法,主要看物件左邊的資料型別,和右邊關係不大! //s2.eat();//Person沒有eat()方法,引用失敗 s1.eat(); ((Student) s2).eat(); } }
package com.oop.demo06; public class Person { public void run(){ System.out.println("run"); } } /* 多型注意事項: 1. 多型是方法的多型,屬性沒有多型 2. 父類和子類,有聯絡 型別轉換異常! ClassCastException! 3. 存在條件:繼承關係,方法需要重寫,父類引用指向子類物件! Father f1 = new Son(); 1. static 方法,屬於類,它不屬於例項 2. final 常量; 3. private 方法; */
package com.oop.demo06;
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
instanceof (型別轉換) 引用型別,判斷一個物件是什麼型別,判斷子父類之間的關係
package com.oop; import com.oop.demo06.Person; import com.oop.demo06.Student; import com.oop.demo06.Teacher; public class Application { public static void main(String[] args) { //Object > String //Object > Person > Teacher //Object > Person > Student Object object = new Student(); //System.out.println(X instanceof Y); //能不能編譯通過! X 與 Y 之間的關係 System.out.println(object instanceof Student); //true System.out.println(object instanceof Person); //true System.out.println(object instanceof Object); //true System.out.println(object instanceof Teacher); //false System.out.println(object instanceof String); //false System.out.println("======================"); Person person = new Student(); System.out.println(person instanceof Student); //true System.out.println(person instanceof Person); //true System.out.println(person instanceof Object); //true System.out.println(person instanceof Teacher); //false //System.out.println(person instanceof String); //編譯報錯! System.out.println("======================="); Student student = new Student(); System.out.println(student instanceof Student); //true System.out.println(student instanceof Person); //true System.out.println(student instanceof Object); //true //System.out.println(student instanceof Teacher); //編譯報錯! //System.out.println(person instanceof String); //編譯報錯! } }
package com.oop.demo06;
public class Person {
}
package com.oop.demo06;
public class Student extends Person{
}
package com.oop.demo06;
public class Teacher extends Person {
}
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
public class Application {
public static void main(String[] args) {
//型別之間的轉換: 父 子
//高 低
Person obj = new Student();
//obj 將這個物件轉換為Student型別,我們就可以使用Student型別的方法了!
//方式一
Student student = (Student) obj;
student.go();
//方式二
((Student) obj).go();
//子類轉換為父類,可能丟失自己的本來的一些方法!
Teacher teacher = new Teacher();
teacher.to();
Person person = teacher;
//person.to(); //提示沒有 to() 方法
}
}
/*
1. 父類引用指向子類的物件
2. 把子類轉換為父類,向上轉型
3. 把父類轉換為子類,向下轉型:強制轉換
4. 方便方法的呼叫,減少重複的程式碼!簡潔
封裝、繼承、多型! 抽象類、介面
*/
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
package com.oop.demo06;
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
package com.oop.demo06;
public class Teacher extends Person {
public void to(){
System.out.println("to");
}
}