1. 程式人生 > >“面向物件的特徵”練習題

“面向物件的特徵”練習題

考核知識點:繼承、this

​
public class Test01 {
    public static void main(String[] args) {
        Father f = new Father();
        Son s = new Son();
        System.out.println(f.getInfo());
        System.out.println(s.getInfo());
        s.setInfo("尚矽谷");
        System.out.println(f.getInfo());
        System.out.println(s.getInfo());
    }
}
class Father{
    private String info = "atguigu";
    public void setInfo(String info){
        this.info = info;
    }
    public String getInfo(){
        return info;
    }
}
class Son extends Father{
    
}

 

第2題

考核知識點:繼承、this、super


public class Test02 {
    public static void main(String[] args) {
        Father f = new Father();
        Son s = new Son();
        System.out.println(f.getInfo());
        System.out.println(s.getInfo());
        s.test();
        System.out.println("-----------------");
        s.setInfo("大矽谷");
        System.out.println(f.getInfo());
        System.out.println(s.getInfo());
        s.test();
    }
}
class Father{
    private String info = "atguigu";
    public void setInfo(String info){
        this.info = info;
    }
    public String getInfo(){
        return info;
    }
}
class Son extends Father{
    private String info = "尚矽谷";
    public void test(){
        System.out.println(this.getInfo());
        System.out.println(super.getInfo());
    }
}

 

 

第3題

考核知識點:繼承、this、super、重寫

 
public class Test03 {
    public static void main(String[] args) {
        Father f = new Father();
        Son s = new Son();
        System.out.println(f.getInfo());
        System.out.println(s.getInfo());
        s.test();
        System.out.println("-----------------");
        s.setInfo("大矽谷");
        System.out.println(f.getInfo());
        System.out.println(s.getInfo());
        s.test();
    }
}
class Father{
    private String info = "atguigu";
    public void setInfo(String info){
        this.info = info;
    }
    public String getInfo(){
        return info;
    }
}
class Son extends Father{
    private String info = "尚矽谷";
    public void setInfo(String info){
        this.info = info;
    }
    public String getInfo(){
        return info;
    }
    public void test(){
        System.out.println(this.getInfo());
        System.out.println(super.getInfo());
    }
}

第4題

考核知識點:屬性與多型無關

package com.atguigu.test04;
​
public class Test04 {
    public static void main(String[] args) {
        A a = new B();
        System.out.println(a.num);
        System.out.println(((B)a).num);
        System.out.println(((A)((B)a)).num);
        System.out.println("-------------------");
        B b = new B();
        System.out.println(b.num);
        System.out.println(((A)b).num);
        System.out.println(((B)((A)b)).num);
    }
}
class A{
    int num = 1;
}
class B extends A{
    int num = 2;
}

 

第5題

考核知識點:類初始化與例項初始化

package com.atguigu.test05;
​
class HelloA{
    public HelloA(){
        System.out.println("HelloA");
    }
    {
        System.out.println("I'm A Class");
    }
    static{
        System.out.println("static A");
    }
}
​
public class HelloB extends HelloA{
    public HelloB(){
        System.out.println("HelloB");
    }
    {
        System.out.println("I'm B Class");
    }
    static{
        System.out.println("static B");
    }
​
    public static void main(String[] args) {
        new HelloB();
    }
​
}
​

 

第6題

考核知識點:例項初始化方法,屬性與多型無關

package com.atguigu.test06;
​
public class Test06 {
    public static void main(String[] args) {
        Father f = new Son();
        System.out.println(f.x);
    }
}
class Father{
    int x = 10;
    public Father(){
        this.print();
        x = 20;
    }
    public void print(){
        System.out.println("Father.x = " + x);
    }
}
class Son extends Father{
    int x = 30;
    public Son(){
        this.print();
        x = 40;
    }
    public void print(){
        System.out.println("Son.x = " + x);//40
    }
}

 

第7題

考核知識點:類初始化,區域性變數與類變數,自增自減

package com.atguigu.test07;
​
public class Test07 {
    static int x, y, z;
​
    static {
        int x = 5;
        x--;
    }
​
    static {
        x--;
    }
​
    public static void main(String[] args) {
        System.out.println("x=" + x);
        z--;
        method();
        System.out.println("result:" + (z + y + ++z));
    }
​
    public static void method() {
        y = z++ + ++z;
    }
}

 

第8題

考核知識點:多型,重寫,例項初始化過程

package com.atguigu.test08;
​
public class Test08 {
    public static void main(String[] args) {
        Base b1 = new Base();
        Base b2 = new Sub();
    }
}
​
class Base {
    Base() {
        method(100);
    }
​
    public void method(int i) {
        System.out.println("base : " + i);
    }
}
​
class Sub extends Base {
    Sub() {
        super.method(70);
    }
​
    public void method(int j) {
        System.out.println("sub : " + j);
    }
}

 

第9題

考核知識點:方法的引數傳遞、final關鍵字

package com.atguigu.test09;
​
public class Test09 {
    public static void main(String[] args) {
        Other o = new Other();
        new Test09().addOne(o);
        System.out.println(o.i);
    }
    
    public void addOne(final Other o){
        o.i++;
    }
}
class Other{
    public int i;
}
 

&n