抽象類與子類(白馬非馬,離堅白)


白馬非馬,是公孫龍提出的一個邏輯問題。感興趣的可以點擊鏈接視頻 https://www.bilibili.com/video/BV1vp4y147sM?from=search&seid=17824925906438171091&spm_id_from=333.337.0.0

主要論證的是白馬非馬這個觀點。

 

那么從集合的角度看這個問題:

馬是概念(抽象類),定義有屬性形態、顏色。
白馬是馬的子類,具有馬特有的顏色(定義了顏色為白)。
白馬屬於馬,白馬不是馬,馬包含白馬。
人們把白馬、黑馬、黃馬等具有相似形態的一類事物統稱為馬,顏色定義了,沒有初始化。好了,白馬符合馬所具有的形態,顏色為白,屬於馬的范疇。

 

A==B的充分必要條件是:A∈B && B∈A     或者A∩B = A   &&  A∩B = B

這里白馬屬於馬,但馬不屬於白馬,所以白馬與馬不相等。

白馬 == 馬  false

白馬 != 馬  true

 

 

 

 

 下面是代碼的:

public abstract class Horse {
    //馬的顏色
    String color;

    //馬所具有的的特征
    protected void feature(){
        System.out.println("馬是一種生物,四條腿走,跑的快,吃草......");
    }

    public String getColor() {
        return color;
    }

    //初始化顏色屬性
    public Horse(String color) {
        this.color = color;
    }

    @Override
    public boolean equals(Object obj) {
        return color.equals(((Horse)obj).color);
    }
}

  

public class WhileHorse extends Horse {
    //初始化顏色
    public WhileHorse(String color) {
        super(color);
    }

    public void fly(){
        System.out.println("白馬特殊技能,會飛.......");
    }

}

  

public class BlackHorse extends Horse {
    //初始化顏色
    public BlackHorse(String color) {
        super(color);
    }

    public void swing(){
        System.out.println("黑馬特殊技能,會游泳.......");
    }

}

  

public class testHorse {
    public static void main(String[] args) {
        Horse horse1 = new BlackHorse("black");
        horse1.feature();
        System.out.println(horse1.getColor());
        specialSkill(horse1);

        System.out.println("--------------------------------------------------------");
        Horse horse2 = new WhileHorse("while");
        horse2.feature();
        System.out.println(horse2.getColor());
        specialSkill(horse2);

        System.out.println("----------------------------------");
        //馬是抽象,概念,沒法直接初始化,需要具體的指向一種類。
        //Horse horse = new Horse();         //wrong, can not be instantiated,白馬與馬沒法比較
        System.out.println(horse1.equals(horse2));
        System.out.println(horse2.equals(new WhileHorse("while")));
    }

    private static void specialSkill(Horse horse1) {
        if (horse1 instanceof BlackHorse) {
            ((BlackHorse) horse1).swing();
        }else if (horse1 instanceof WhileHorse) {
            ((WhileHorse) horse1).fly();
        }
    }
}

  結果:

馬是一種生物,四條腿走,跑的快,吃草......
black
黑馬特殊技能,會游泳.......
--------------------------------------------------------
馬是一種生物,四條腿走,跑的快,吃草......
while
白馬特殊技能,會飛.......
----------------------------------
false
true

 

離堅白

順便也看了下離堅白。

公孫龍在《堅白論》中的核心論點是“離堅白”,即“堅”與“白”兩種屬性是分離的,不依賴於具體事物而存在,獨立自藏。“離也者,藏也。”

一塊石頭,用手觸碰感覺硬則是堅石,藏了白,為硬石。

一塊石頭,通過眼睛視覺可以看到白色,藏了硬,為白石。

堅白石,不可三分“堅”、“白”、“石”;可二分“堅石”、“白石”。

理解應該不到位,下面代碼就當看下吧

public abstract class Stone {
    private String color;
    private String hardness;

    public Stone(String color, String hardness) {
        this.color = color;
        this.hardness = hardness;
    }

    @Override
    public String toString() {
        return "Stone{" +
                "color='" + color + '\'' +
                ", hardness='" + hardness + '\'' +
                '}';
    }

}

public class ConcreteStone extends Stone{
public ConcreteStone(String color, String hardness) {
super(color, hardness);
}

static class Builder{
private String color;
private String hardness;

public Builder seeColor(String color){
this.color = color;
return this;
}

public Builder touchHardness(String hardness){
this.hardness = hardness;
return this;
}

public Stone buildStone(){
return new ConcreteStone(this.color, this.hardness);
}

}
}

public class StoneTest {

public static void main(String[] args) {
Stone whileStone = new ConcreteStone.Builder().seeColor("while").buildStone();
Stone hardStone = new ConcreteStone.Builder().touchHardness("hard").buildStone();
Stone hardWhileStone = new ConcreteStone.Builder().touchHardness("hard").seeColor("while").buildStone();
System.out.println(whileStone);
System.out.println(hardStone);
System.out.println(hardWhileStone);
Stone concreteStone = new ConcreteStone.Builder().buildStone();
System.out.println(concreteStone);
}
}
 

 

最后:代碼其實也是一種語言,反映了事物的屬性、行為,還有邏輯。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM