ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2018 cs61b dis4

2022-05-25 21:01:50  阅读:213  来源: 互联网

标签:dis4 void cs61b System 2018 m2 println public out


https://sp18.datastructur.es/materials/discussion/disc04.pdf

    public class Animal{
        protected String name, noise;
        protected int age;
        public Animal(String name, int age){
            this.name = name;
            this.age = age;
            this.noise = "Huh?";
        }

        public String makeNoise(){
            if (age < 5) {
                return noise.toUpperCase();
            } else{
                return noise;
            }
        }

        public void greet(){
            System.out.println("Animal " + name + " says: " + makeNoise());
        }
    }
    public class Cat extends Animal{
        public Cat(String name){
            this.name = name;
            this.noise = "Meow!"
        }
        @Override
        public void greet(){
            System.out.println("Cat " + name + " says: " + makeNoise());
        }
    }
    /**
    (A) Animal Pluto says: Huh?
    (B) Cat Garfield says: Meow!
    (C) Dof Fido says: WOOF!
    (D) Cat Garfield says: Meow!
    (E) Cat Garfield says: Meow!
    //a是一个Animal类,而d是一个Dog,不是直接赋值,需要进行一个强制转换。
    */
     class A {
            public int x = 5;
            public void m1() {System.out.println("Am1-> " + x);}
            public void m2() {System.out.println("Am2-> " + this.x);}
            public void update() { x = 99;}
     }
     class B extends A {
            public void m2() {System.out.println("Bm2-> " + x);}
            public void m2(int y) {System.out.println("Bm2y-> " + y);}
            public void m3() {System.out.println("Bm3-> " + "called");}
     }
     class C extends B {
            public int y = x + 1;
            public void m2() {System.out.println("Cm2-> " + super.x);}
            public void m4() {System.out.println("Cm4-> " + super.super.x); }} //can't do super.super
            public void m5() {System.out.println("Cm5-> " + y);}
     }
     class D {
        public static void main (String[] args) {
            // B a0 = new A(); Dynamic type must be B or subclass of B
            // a0.m1(); cascading: prev line failed, so a0 can't be initialized
            // a0.m2(16); cascading: prev line failed, so a0 can't be initialized
            A b0 = new B();
            System.out.println(b0.x); [prints "5"]
            b0.m1(); //[prints "Am1-> 5"]
            b0.m2(); //[prints "Bm2-> 5"]
            // b0.m2(61); m2 (int y) not defined in static type of b0
            B b1 = new B();
            b1.m2(61); //[prints "Bm2y-> 61"]
            b1.m3(); //[prints "Bm3-> called"]
            A c0 = new C();
            c0.m2(); //[prints "cm2-> 5"]
            // C c1 = (A) new C(); Can't assign c1 to an A
            A a1 = (A) c0;
            C c2 = (C) a1;
            c2.m3(); [print Bm3-> called]
            // c2.m4(); C.m4() is invalid
            c2.m5(); //[print Cm5-> 6]
            ((C) c0).m3(); //[print Bm3-> called]
            Inheritance
            //(C) c0.m3(); NOT RUNTIME ERROR This would case the result of what the method returns and it returns void therefore compile-time error
            b0.update();
            b0.m1(); [print Am1-> 99]
        }
     }

标签:dis4,void,cs61b,System,2018,m2,println,public,out
来源: https://www.cnblogs.com/echoT/p/16310795.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有