您现在的位置是:主页 > news > 漳州做网站的公司/网站页面
漳州做网站的公司/网站页面
admin2025/5/23 7:07:59【news】
简介漳州做网站的公司,网站页面,音乐类网站开发,枣庄网站建设电话1:什么是多態一個對象的多種狀態(老師)(員工)(兒子)教師 a 老鍾;員工 b 老鍾;2:多態體現父類引用變量指向了子類的對象 Father f new Son();父類引用也可以接受自己的子類對象1:Father類1:非靜態成員變量x2:…
1:什么是多態
一個對象的多種狀態
(老師)(員工)(兒子)
教師 a =老鍾;
員工 b =老鍾;
2:多態體現
父類引用變量指向了子類的對象 Father f = new Son();
父類引用也可以接受自己的子類對象
1:Father類
1:非靜態成員變量x
2:靜態成員變量y
3:非靜態方法eat,方法體輸出父類信息
4:靜態方法speak();方法體輸出父類信息
class Father {
int x = 1;
static int y = 2;
void eat() {
System.out.println("開吃");
}
static void speak() {
System.out.println("小頭爸爸");
}
}
2:Son類
1:非靜態成員變量x
2:靜態成員變量y
3:非靜態方法eat,方法體輸出子類信息
4:靜態方法speak();方法體輸出子類信息
class Son extends Father {
int x = 3;
static int y = 4;
void eat() {
System.out.println("大頭兒子很能吃");
}
static void speak() {
System.out.println("大頭兒子。");
}
}
class Demo1 {
public static void main(String[] args) {
Father f = new Son(); // 父類引用指向了子類對象。
System.out.println(f.x); // 1
System.out.println(f.y); // 2
f.eat(); // 輸出的是子類的。
f.speak(); // 輸出的是父類
}
}
3總結:
1:當父類和子類具有相同的非靜態成員變量,那么在多態下訪問的是父類的成員變量
2:當父類和子類具有相同的靜態成員變量,那么在多態下訪問的是父類的靜態成員變量
所以:父類和子類有相同的成員變量,多態下訪問的是父類的成員變量。
3:當父類和子類具有相同的非靜態方法(就是子類重寫父類方法),多態下訪問的是子類的成員方法。
4:當父類和子類具有相同的靜態方法(就是子類重寫父類靜態方法),多態下訪問的是父類的靜態方法。
多態前提
1:類與類之間有關系,繼承或者實現
多態弊端
1:提高擴展性,但是只能使用父類引用指向父類成員。
多態特點
非靜態
1:編譯時期,參考引用型變量所屬的類是否有調用的方法,如果有編譯通過。沒有編譯失敗
2:運行時期,參考對象所屬類中是否有調用的方法。
3:總之成員函數在多態調用時,編譯看左邊,運行看右邊。
在多態中,成員變量的特點,無論編譯和運行 參考左邊(引用型變量所屬的類)。
在多態中,靜態成員函數特點,無論編譯和運行都參考左邊
多態練習
1:多態可以作為形參,接受范圍更廣的對象,避免函數重載過度使用。
1:定義功能,根據輸出任何圖形的面積和周長。
abstract class MyShape{
public abstract double getArea();
public abstract double getLen();
}
2:定義Rect類繼承MyShape
class Rect extends MyShape{
double width ;
double height;
Rect(){
}
Rect(double width ,double height){
this.width=width;
this.height=height;
}
public double getArea(){
return width*height;
}
public double getLen(){
return 2*(width+height);
}
}
3:定義Cricle類繼承MyShape
class Circle extends MyShape{
double r;
public static final double PI=3.14;
Circle(){
}
Circle(double r){
this.r=r;
}
public double getLen(){
return 2*PI*r;
}
public double getArea(){
return PI*r*r;
}
}
4:定義靜態方法計算任意圖形的面積和周長
1:未知內容參與運算,不能確定用戶傳入何種圖形,使用多態。
1:形參定義為 MyShape my
2:調用計算面積方法,和計算周長方法。並打印
2:使用多態特性,子類重寫了父類非靜態方法,會執行子類的方法。
class Demo2{
public static void main(String[] args){
System.out.println();
print(new Rect(3,4)); //MyShape m =new Rect(3,4);
print(new Circle(3));
}
//根據用戶傳入的圖形對象,計算出該圖形的面積和周長
//1:多態可以作為形參,接受范圍更廣的對象,避免函數重載過度使用。
public static void print(MyShape m){
System.out.println(m.getLen());
System.out.println(m.getArea());
}
}
2:多態可以作為返回值類型。
獲取任意一輛車對象
1:定義汽車類,有名字和顏色,提供有參和無參構造,有運行的行為。
class Car {
String name;
String color;
Car() {
}
Car(String name, String color) {
this.name = name;
this.color = color;
}
void run() {
System.out.println("跑跑。。。。");
}
}
2:定義Bmw類,繼承Car類,提供無參構造和有參構造(super父類構造),重寫父類運行行為。
class Bmw extends Car {
Bmw() {
}
Bmw(String name, String color) {
super(name, color);
}
void run() {
System.out.println("寶馬很拉風。。。。");
}
}
3:定義Benz類,繼承Car類,提供無參構造和有參構造(super父類構造),重寫父類運行行為。
class Benz extends Car {
Benz() {
}
Benz(String name, String color) {
super(name, color);
}
void run() {
System.out.println("奔馳商務首選。。。。");
}
}
4:定義Bsj類,繼承Car類,提供無參構造和有參構造(super父類構造),重寫父類運行行為。
class Bsj extends Car {
Bsj() {
}
Bsj(String name, String color) {
super(name, color);
}
void run() {
System.out.println("泡妞首選。。。。");
}
}
5:定義靜態方法,汽車工廠,隨機生產汽車。使用多態定義方法返回值類型。
1:使用(int)Math.round(Math.random()*2); 生成0-2之間隨機數。
2:使用if else 判斷,指定,0,1,2 new 不同汽車 並返回。
class Demo3 {
public static void main(String[] args) {
int x = 0;
while (x < 100) {
Car c = CarFactory();
c.run();
x++;
}
}
// 定義靜態方法,汽車工廠,隨機生產汽車。使用多態定義方法返回值類型。
// 使用隨機數,0.1.2 if 0 bsj 1 bmw 2 bc
public static Car CarFactory() {
int x = (int) Math.round(Math.random() * 2);
if (0 == x) {
return new Bmw("寶馬x6", "紅色");
} else if (1 == x) {
return new Benz("奔馳", "黑色");
} else if (2 == x) {
return new Bsj("保時捷", "棕色");
} else {
return new Benz("Smart", "紅色");
}
}
}
3:抽象類和接口都可以作為多態中的父類引用類型。 多態之類型轉型
1:案例定義Father類
1:定義method1和method2方法
class Father {
void method1() {
System.out.println("這是父類1");
}
void method2() {
System.out.println("這是父類2");
}
}
2:定義Son類繼承Father類
1:定義method1(重寫父類method1)和method2方法
class Son extends Father {
void method1() {
System.out.println("這是子類1");
}
void method3() {
System.out.println("這是子類3");
}
}
3:創建Father f=new Son();
1: f.method1() 調用的子類或者父類?
2: f.method2() 編譯和運行是否通過?
3: f.method3() 編譯和運行是否通過?(編譯報錯)
class Demo4 {
public static void main(String[] args) {
Father f = new Son();
f.method1(); // 這是子類1
f.method2(); // 這是父類2
// f.method3(); //編譯報錯。
// 多態弊端,只能使用父類引用指向父類成員。
// 類類型轉換
Son s = (Son) f;
s.method3();
System.out.println();
}
}
4:如何在多態下,使用父類引用調用子類特有方法。
1:基本類型轉換:
1:自動:小->大
2:強制:大->小
2:類類型轉換
前提:繼承,必須有關系
1:自動:子類轉父類
2:強轉:父類轉子類
3:類型轉換
1:Son s=(Son)f
2:s.method3();
案例:
1:定義Animal類顏色成員變量,無參構造,有參構造,run方法
class Animal {
String color;
Animal() {
}
Animal(String color) {
this.color = color;
}
void run() {
System.out.println("跑跑");
}
}
2:定義Dog類,繼承Animal,定義無參構造,有參構造(使用super調用父類有參構造),Dog的特有方法ProtectHome
class Dog extends Animal {
Dog() {
}
Dog(String color) {
super(color);
}
void run() {
System.out.println("狗兒跑跑");
}
void protectHome() {
System.out.println("旺旺,看家");
}
}
3:定義Fish類,繼承Animal,定義無參構造,有參構造(使用super調用父類有參構造),Fish特有方法swim
class Fish extends Animal {
Fish() {
}
Fish(String color) {
super(color);
}
void run() {
System.out.println("魚兒水中游");
}
void swim() {
System.out.println("魚兒游泳");
}
}
4:使用多態,Animal a=new Dog();
5:調用Dog的特有方法,ProtectHome
1:類類型轉換,Dog d=(Dog)a;
2:d.protectHome
class Demo5 {
public static void main(String[] args) {
Animal ani = new Dog();
// ani.protectHome();
// 正常轉換
Dog d = (Dog) ani;
d.protectHome();
// 多態例外
Animal an = new Animal();
// ClassCastException
// Dog d=(Dog)an
// 多態例外
Animal dog = new Dog();
// ClassCastException
// Fish f = (Fish) dog;
}
}
6:非多態
1:Animal a=new Animal();
2:類類型轉換
Dog d=(Dog)a;
d.protectHome();
3:編譯通過,運行出現異常
1:ClassCastException
7:多態例外
1:Animal a=new Dog();
2:類類型轉換
1:Fish f=(Fish)a;
2:f.fish();
3:編譯通過,運行異常
1:ClassCastException
4:雖然是多態,但是狗不能轉為魚,他們之間沒有關系。
案例2
1:定義一功能,接收用戶傳入動物,根據用於傳入的具體動物,執行該動物特有的方法
2:使用多態,方法形參,不能確定用戶傳入的是那種動物
3:使用instanceof 關鍵字,判斷具體是何種動物,
4:類轉換,執行該動物的特有方法。
class Animal {
String color;
Animal() {
}
Animal(String color) {
this.color = color;
}
void run() {
System.out.println("跑跑");
}
}
class Dog extends Animal {
Dog() {
}
Dog(String color) {
super(color);
}
void run() {
System.out.println("狗兒跑跑");
}
void protectHome() {
System.out.println("旺旺,看家");
}
}
class Fish extends Animal {
Fish() {
}
Fish(String color) {
super(color);
}
void run() {
System.out.println("魚兒水中游");
}
void swim() {
System.out.println("魚兒游泳");
}
}
class Bird extends Animal {
Bird() {
}
Bird(String color) {
super(color);
}
void run() {
System.out.println("鳥兒空中飛");
}
void fly() {
System.out.println("我是一只小小鳥。。。。");
}
}
class Demo6 {
public static void main(String[] args) {
System.out.println();
doSomething(new Dog());
doSomething(new Bird());
doSomething(new Fish());
}
// 定義一功能,接收用戶傳入動物,根據用於傳入的具體動物,執行該動物特有的方法
public static void doSomething(Animal a) {
if (a instanceof Dog) {
Dog d = (Dog) a;
d.protectHome();
} else if (a instanceof Fish) {
Fish f = (Fish) a;
f.swim();
} else if (a instanceof Bird) {
Bird b = (Bird) a;
b.fly();
} else {
System.out.println("over");
}
}
}