“面向对象的特征”练习题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

“面向对象的特征”练习题

可乐葱花   2020-02-18 我要评论

考核知识点:继承、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;
}
 

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们