Exemple #1
0
    // Use this for initialization
    void Start()
    {
        // Let's illustrate inheritance with the default constructors.
        Debug.Log("Creating the fruit");
        Fruit myFruit = new Fruit();

        Debug.Log("Creating the apple");
        Apple myApple = new Apple();

        // // Call the methods of the Fruit class
        // myFruit.SayHello();
        // myFruit.Chop();

        // // Call the methods of the Apple class.
        // // Notice how Class Apple has access to all of the public methods of Class Fruit.
        // myApple.SayHello();
        // myApple.Chop();

        // Call the methods of the Apple class.
        // Notice how class Apple has access to all of the public methods of Class Fruit.
        Debug.Log("Creating the Fruit");
        myFruit = new Fruit("yellow");
        Debug.Log("Creating the apple");
        myApple = new Apple("green");

        // Call the methods of the Fruit class
        myFruit.SayHello();
        myFruit.Chop();

        // Call the methods of the Apple class.
        // Notice how class Apple has access to all of the public methods of class Fruit.
        myApple.SayHello();
        myApple.Chop();
    }
Exemple #2
0
    void Start()
    {
        Apple myApple = new Apple();

        myApple.SayHello();
        myApple.Chop();

        // Upcasting.
        Fruit myFruit = new Apple();

        // Even the type is Fruit, the functions in
        // the Fruit class are virtual, so the Apple
        // class functions will be called.
        myFruit.SayHello();
        myApple.Chop();
    }
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Creating the fruit");
        Fruit myFruit = new Fruit();

        Debug.Log("Creating the apple");
        Apple myApple = new Apple();

        myFruit.SayHello();
        myFruit.Chop();

        myApple.SayHello();
        myApple.Chop();

        Debug.Log("Creating the fruit");
        myFruit = new Fruit("yellow");
        Debug.Log("Creating the apple");
        myApple = new Apple("green");

        myFruit.SayHello();
        myFruit.Chop();

        myApple.SayHello();
        myApple.Chop();
    }
Exemple #4
0
    void Start()
    {
        Debug.Log("~~~~~~~~~ myApple ~~~~~~~~~~~~~~~~~~~~~~~~");

        Apple myApple = new Apple();

        //Notice that the Apple version of the methods
        //override the fruit versions. Also notice that
        //since the Apple versions call the Fruit version with
        //the "base" keyword, both are called.
        myApple.SayHello();
        myApple.Chop();

        Debug.Log("~~~~~~~~ myFruit ~~~~~~~~~~~~~~~~~~~~~~~~~");

        //Overriding is also useful in a polymorphic situation.
        //Since the methods of the Fruit class are "virtual" and
        //the methods of the Apple class are "override", when we
        //upcast an Apple into a Fruit, the Apple version of the
        //Methods are used.
        Fruit myFruit = new Apple();

        myFruit.SayHello();
        myFruit.Chop();


        Debug.Log("~~~~~~~~~ myFruit 2 ~~~~~~~~~~~~~~~~~~~~~~~~");

        Fruit myFruit2 = new Fruit();

        myFruit2.SayHello();
        myFruit2.Chop();
    }
Exemple #5
0
    // Use this for initialization
    void Start()
    {
        //we made fruit a public class so we can create fruit from any script in the project
        Fruit myFruit = new Fruit("lemon", "sweet", "brown", 2);
        Apple myApple = new Apple();

        //these two chop functions will behhave deifferently the apple chop has been identified as a new fuction that will
        //deviate from the parent
        myFruit.Chop();
        myApple.Chop();
        //this will downgrade my apple to be a fruit
        //this is callied variable casting
        Fruit faleApple = (Fruit)myApple;
    }
    void Start()
    {
        Apple myApple = new Apple();

        // Apple versions of the methods override the fruit version.
        // Since Apple's versions call the Fruit version with the "base" keyword, both are called.
        myApple.SayHello();
        myApple.Chop();

        // Overriding is also useful in a polymorphic situation.
        // Since the methods of the Fruit class are "virtual" and the methods of the Apple class are "override",
        // when we upcast an Apple into a Fruit, teh Apple version of the Methods are used.
        Fruit myFruit = new Apple();

        myFruit.SayHello();
        myFruit.Chop();
    }
Exemple #7
0
    void Start()
    {
        // This is called Upcasting.
        Fruit myFruit = new Apple();

        // Since the type is "Fruit",
        // the function will be called from the
        // parent class.
        myFruit.SayHello();
        myFruit.Chop();

        // Class re-specified for
        // using functions in child class.
        // This is called Downcasting.
        Apple myApple = (Apple)myFruit;

        myApple.SayHello();
        myApple.Chop();
    }
    void Start()
    {
        Apple myApple = new Apple();

        //请注意,Apple 版本的方法
        //将覆盖 Fruit 版本。另外请注意,
        //由于 Apple 版本使用“base”关键字
        //来调用 Fruit 版本,因此两者都被调用。
        myApple.SayHello();
        myApple.Chop();

        //“覆盖”在多态情况下也很有用。
        //由于 Fruit 类的方法是“虚”的,
        //而 Apple 类的方法是“覆盖”的,因此
        //当我们将 Apple 向上转换为 Fruit 时,
        //将使用 Apple 版本的方法。
        Fruit myFruit = new Apple();

        myFruit.SayHello();
        myFruit.Chop();
    }
Exemple #9
0
    void Start()
    {
        //请注意,这里的变量“myFruit”的类型是
        //Fruit,但是被分配了对 Apple 的引用。这是
        //由于多态而起作用的。由于 Apple 是 Fruit,
        //因此这样是可行的。虽然 Apple 引用存储
        //在 Fruit 变量中,但只能像 Fruit 一样使用
        Fruit myFruit = new Apple();

        myFruit.SayHello();
        myFruit.Chop();

        //这称为向下转换。Fruit 类型的变量“myFruit”
        //实际上包含对 Apple 的引用。因此,
        //可以安全地将它转换回 Apple 变量。这使得
        //它可以像 Apple 一样使用,而在以前只能像 Fruit
        //一样使用。
        Apple myApple = (Apple)myFruit;

        myApple.SayHello();
        myApple.Chop();
    }
    void Start()
    {
        //Notice here how the variable "myFruit" is of type
        //Fruit but is being assigned a reference to an Apple. This
        //works because of Polymorphism. Since an Apple is a Fruit,
        //this works just fine. While the Apple reference is stored
        //in a Fruit variable, it can only be used like a Fruit
        Fruit myFruit = new Apple();

        myFruit.SayHello();
        myFruit.Chop();

        //This is called downcasting. The variable "myFruit" which is
        //of type Fruit, actually contains a reference to an Apple. Therefore,
        //it can safely be turned back into an Apple variable. This allows
        //it to be used like an Apple, where before it could only be used
        //like a Fruit.
        Apple myApple = (Apple)myFruit;

        myApple.SayHello();
        myApple.Chop();
    }
    void Start()
    {
        //让我们用默认构造函数
        //来说明继承。
        Debug.Log("Creating the fruit");
        Fruit myFruit = new Fruit();

        Debug.Log("Creating the apple");
        Apple myApple = new Apple();

        //调用 Fruit 类的方法。
        myFruit.SayHello();
        myFruit.Chop();

        //调用 Apple 类的方法。
        //注意 Apple 类如何访问
        //Fruit 类的所有公共方法。
        myApple.SayHello();
        myApple.Chop();

        //现在,让我们用读取字符串的
        //构造函数来说明继承。
        Debug.Log("Creating the fruit");
        myFruit = new Fruit("yellow");
        Debug.Log("Creating the apple");
        myApple = new Apple("green");

        //调用 Fruit 类的方法。
        myFruit.SayHello();
        myFruit.Chop();

        //调用 Apple 类的方法。
        //注意 Apple 类如何访问
        //Fruit 类的所有公共方法。
        myApple.SayHello();
        myApple.Chop();
    }
    private static void Inheritance()
    {
        //Let's illustrate inheritance with the
        //default constructors.
        Debug.Log("Creating the fruit");
        Fruit myFruit = new Fruit();

        Debug.Log("Creating the apple");
        Apple myApple = new Apple();

        //Call the methods of the Fruit class.
        myFruit.SayHello();
        myFruit.Chop();

        //Call the methods of the Apple class.
        //Notice how class Apple has access to all
        //of the public methods of class Fruit.
        myApple.SayHello();
        myApple.Chop();

        //Now let's illustrate inheritance with the
        //constructors that read in a string.
        Debug.Log("Creating the fruit");
        myFruit = new Fruit("yellow");
        Debug.Log("Creating the apple");
        myApple = new Apple("green");

        //Call the methods of the Fruit class.
        myFruit.SayHello();
        myFruit.Chop();

        //Call the methods of the Apple class.
        //Notice how class Apple has access to all
        //of the public methods of class Fruit.
        myApple.SayHello();
        myApple.Chop();
    }
Exemple #13
0
    void Start()
    {
        /*
         * Notice here how the variable "myFruit" is of type
         * Fruit but is being assigned a reference to an Apple. This
         * works because of Polymorphism. Since an Apple is a Fruit,
         * this works just fine. While the Apple reference is stored
         * in a Fruit variable, it can only be used like a Fruit
         */
        Fruit_polymorphism myFruit = new Apple();

        myFruit.SayHello();
        myFruit.Chop();

        /*
         * This is called downcasting. The variable "myFruit" which is
         * of type Fruit, actually contains a reference to an Apple. Therefore,
         * it can safely be turned back into an Apple variable. This allows
         * it to be used like an Apple, where before it could only be used
         * like a Fruit.
         */
        Apple myApple = (Apple)myFruit;

        myApple.SayHello();
        myApple.Chop();

        // Notice how each Humanoid variable contains
        // a reference to a different class in the
        // inheritance hierarchy, yet each of them
        // calls SayHello() method.

        // Apple myRipApple  = new RipeApple();
        Fruit_polymorphism myRipApple = new RipeApple();

        myRipApple.SayHello();
    }
Exemple #14
0
    void Start()
    {
        // Let's illustrate inheritance with the default constructors
        Debug.Log("Creating the fruit");
        Fruit myFruit = new Fruit();

        Debug.Log("Creating the apple");
        Apple myApple = new Apple();

        // Call the methods of the Fruit class
        myFruit.SayHello();
        myFruit.Chop();

        // Call the methods of the Apple class
        // Notice how class Apple has access to all
        // of all the public methods of class Fruit
        myApple.SayHello();
        myApple.Chop();

        // Illustrate inheritance with constructors
        Debug.Log("Creating the fruit");
        myFruit = new Fruit("yellow");
        Debug.Log("Creating the apple");
        myApple = new Apple("green");

        // Call the methods of the Fruit class.
        myFruit.SayHello();
        myFruit.Chop();

        // Call the methods of the Apple class
        // Notice how class Apple has access to all
        // of the public methods of class Fruit
        myApple.SayHello();
        myApple.Chop();

        // Notice here how the variable "myFruit" is of type
        // Fruit but is being assigned a reference to an Apple. This
        // works because of Polymorphism. Since an Apple is a Fruit,
        // this works just fine. While the Apple reference is stored
        // in a Fruit variable, it can only be used like a Fruit
        Fruit myFruit = new Apple();

        myFruit.SayHello();
        myFruit.Chop();

        // This is called downcasting. The variable "myFruit" which is
        // of type Fruit, actually contains a reference to an Apple. Therefore,
        // it can safely be turned back into an Apple variable. This allows
        // it to be used like an Apple, where before it could only be used
        // like a Fruit
        Apple myApple = (Apple)myFruit;

        myApple.SayHello();
        myApple.Chop();

        // Overriding is also useful in a polymorphic situation
        // Since the methods of the Fruit class are "virtual" and
        // the methods of the Apple class are "override", when we
        // upcast an Apple into a Fruit, the Apple version of the
        // methods is used
        Fruit myFruit = new Apple();

        myFruit.SayHello();
        myFruit.Chop();
    }