codecamp

Super 程序题

题目一

class Base{
    Base(){
        System.out.println("Base");
    }
}


public class Checket extends Base{
    Checket(){
        System.out.println("Checket");
        super();
    }
    public static void main(String argv[]){
        Checket a = new Checket();
    }
}

输出是什么? 是 compile time error. super() 必须放在前面.

放在前面之后,输出为 Base Checket

题目二

import java.util.Date;


public class Test extends Date{


    public static void main(String[] args) {
       new Test().test();
    }


    public void test(){
       System.out.println(super.getClass().getName());
    }
}

返回的结果是 Test

因为super.getClass().getName() 调用了父类的 getClass() 方法, 返回当前类

如果想得到父类的名称,应该用如下代码:

getClass().getSuperClass().getName()

题目三

public abstract class Car {


    String name = "Car";


    public String getName(){
        return name;
    }


    public abstract void demarre();
}


public class B extends Car{
    String name = "B";


    public String getName(){
        return name;
    }


    public void demarre() {
        System.out.println(getName() + " demarre");
    }
}


public class C extends B{
    String name = "C";


    public String getName(){
        return name;
    }


    public void demarreWithSuper() {
        System.out.println(super.getName() + " demarre");
    }


    public void demarreNoSuper() {
        System.out.println(getName() + " demarre");
    }
}


public class D extends B{
    public String getName(){
        return name;
    }


    public void demarreNoSuper() {
        System.out.println(getName() + " demarre");
    }
}


public class Test {
    public static void main(String[] args) {
        B b = new B();
        b.demarre();


        Car bCar = new B();
        bCar.demarre();


        C c = new C();
        c.demarre(); // c 里并没有定义这个函数
        c.demarreWithSuper();
        c.demarreNoSuper();


        D d = new D();
        d.demarre();


        transfer(c);    // TransferC
        transfer((B)c); // TransferB
        transfer(d);    // TransferB
    }


        public static void transfer(B b){
            System.out.println("TransferB");
            b.demarre();
        }


        public static void transfer(C c){
            System.out.println("TransferC");
            c.demarre();
        }
    }
}

输出是 B demarre B demarre C demarre B demarre C demarre B demarre TransferC C demarre TransferB C demarre TransferB B demarre

super 关键词
this 程序题
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Java 面试笔记

Java

多态 Polymorphism

Static 关键字

int 与 integer

Programme

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }