多態(tài)
基本介紹
- 即同一方法可以根據(jù)發(fā)送對(duì)象的不同而采用多種不同的行為方式
- 一個(gè)對(duì)象的實(shí)際類型是確定的,但是可以指向?qū)ο蟮囊妙愋涂梢院芏?/li>
- 多態(tài)存在的條件:a.有繼承關(guān)系;b.子類重寫父類方法;c.父類引用指向子類對(duì)象
- 多態(tài)注意事項(xiàng):
- 多態(tài)是方法的多態(tài),屬性沒有多態(tài)
- 父類和子類,有聯(lián)系 類型轉(zhuǎn)換異常(ClassCastException)
- 存在條件:繼承關(guān)系;子類重寫父類方法;父類引用指向子類對(duì)象
- 還有三種方法不能重寫:1.static方法(屬于類,不屬于實(shí)例)2.fianl方法(常量,不可改變)3.private方法(私有方法)
運(yùn)用實(shí)例
DuoTai.java
public class DuoTai {
public static void main(String[] args) {
//一個(gè)對(duì)象的實(shí)際類型是確定的
//new Student();
//new Person();
//可以指向的引用類型就不確定了
//Student能調(diào)用的方法都是自己或者繼承父類的
Student s1 = new Student();
//Person父類型,可以指向子類,但是不能調(diào)用子類的獨(dú)有方法
Person s2 = new Student();//父類的引用指向子類
Object s3 = new Student();
//對(duì)象能執(zhí)行那些方法,主要看對(duì)象左邊的類型,和右邊關(guān)系不大
s1.run();//當(dāng)子類的方法為空時(shí),子類調(diào)用的是父類的方法
s2.run();//當(dāng)子類重寫了父類的方法時(shí),父類調(diào)用的方法是子類的方法
s1.son();
//s2.son();
}
}
父類:Person.java
public class Person {
public void run(){
System.out.println("run");
}
}
子類:Student.java
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
public void son(){
System.out.println("son2");
}
}
Instanceof關(guān)鍵字的作用
public class Instanceof {
public static void main(String[] args) {
//Object>String
//Object>Person>Student
//Object>Person>Teacher
//System.out.println(x instanceof y);代表能不能通過
Object object=new Student();
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(Student和Teacher沒有繼承關(guān)系,屬于平級(jí),父類都是Person)
System.out.println(object instanceof String);//false(Student和String沒有繼承關(guān)系)
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);//編譯報(bào)錯(cuò)(因?yàn)閜erson和String沒有關(guān)系)
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);//編譯報(bào)錯(cuò)(因?yàn)镾tudent和Teacher沒有關(guān)系)
//System.out.println(student instanceof String);//編譯報(bào)錯(cuò)(因?yàn)镾tudent和String沒有關(guān)系)
}
}
父類:Person.java
public class Person {
public void run(){
System.out.println("run");
}
}
子類:Student.java
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
public void son(){
System.out.println("son2");
}
}
子類:Teacher.java
public class Teacher extends Person {
}
類型轉(zhuǎn)換
基本介紹
- 父類引用指向子類的對(duì)象
- 把子類轉(zhuǎn)換成父類,向上轉(zhuǎn)換
- 把父類轉(zhuǎn)換成子類,向下轉(zhuǎn)換,強(qiáng)制轉(zhuǎn)換
- 方便方法的調(diào)用,減少重復(fù)的代碼,簡介
運(yùn)用實(shí)例
public class LeiXing {
public static void main(String[] args) {
//類型之間的轉(zhuǎn)換:父類 子類
Student student=new Student();
student.son();
//子類轉(zhuǎn)換成父類,可能會(huì)丟失自己本來的一些方法
Person person=student;
//person.son();(編譯報(bào)錯(cuò),無法使用son()方法了)
//父類轉(zhuǎn)換成子類,強(qiáng)制轉(zhuǎn)換
((Student)person).son();//第一種方法
Student person1= (Student) person;//第二種方法
person1.son();
}
}
static關(guān)鍵字詳解
實(shí)例1
public class Static {
private static int age;//靜態(tài)屬性
private int score;//非靜態(tài)屬性
public static void run(){//靜態(tài)方法
}
public void go(){//非靜態(tài)方法
}
public static void main(String[] args) {
Static s1 = new Static();
System.out.println(Static.age);//靜態(tài)屬性可以直接通過類名來調(diào)用
//System.out.println(Static.score);//編譯報(bào)錯(cuò)
System.out.println(s1.score);//非靜態(tài)屬性則需要通過對(duì)象.屬性來調(diào)用
System.out.println(s1.age);//靜態(tài)屬也可以通過對(duì)象.屬性來調(diào)用
//方法和屬性的性質(zhì)相同
run();
Static.run();
//Static.go();
s1.go();
}
}
實(shí)例2
public class Static2 {
//第二執(zhí)行(一般是用來賦初值)
{
System.out.println("匿名代碼塊");
}
//最先執(zhí)行1(只執(zhí)行一次)
static {
System.out.println("靜態(tài)代碼塊");
}
//第三執(zhí)行
public Static2() {
System.out.println("構(gòu)造方法");
}
public static void main(String[] args) {
Static2 static2 = new Static2();
System.out.println("=============");
Static2 static3 = new Static2();
}
}
運(yùn)行結(jié)果:
靜態(tài)代碼塊
匿名代碼塊
構(gòu)造方法
=============
匿名代碼塊
構(gòu)造方法
實(shí)例3
//靜態(tài)包導(dǎo)入
import static java.lang.Math.random;
public class Static3 {
public static void main(String[] args) {
//System.out.println(Math.random());
System.out.println(random());//可直接使用random方法
}
}
抽象類
基本介紹
- abstract修飾符可以用來修飾方法也可以修飾類,如果修飾方法則該方法就是抽象方法,如果是修飾類則該類就是抽象類
- 抽象類中可以沒有抽象方法,但是抽象方法的類必須是抽象類
- 抽象類,不能使用new關(guān)鍵字來創(chuàng)建對(duì)象,他是用來讓子類繼承的,沒有構(gòu)造器
- 抽象方法,只有方法聲明,沒有方法的實(shí)現(xiàn),他是讓子類實(shí)現(xiàn)的
- 子類繼承抽象類,那么必須實(shí)現(xiàn)抽象類沒有實(shí)現(xiàn)的抽象方法,否則該子類必須聲明為抽象類
- 抽象為單繼承,接口則可以多繼承
運(yùn)用實(shí)例
//abstract抽象類
public abstract class Abstract {
//約束,有人幫我們實(shí)現(xiàn)
//抽象方法,只有方法聲明,沒有方法的實(shí)現(xiàn)
public abstract void doSome();
}
public class Abs extends Abstract{
//子類繼承抽象類,那么必須實(shí)現(xiàn)抽象類沒有實(shí)現(xiàn)的抽象方法,否則該子類必須聲明為抽象類
@Override
public void doSome() {
}
}
浙公網(wǎng)安備 33010602011771號(hào)