JAVA基础学习--面向对象试题

来源:互联网 发布:linux登录密码修改 编辑:程序博客网 时间:2024/06/07 12:11

一、异常捕获

 

复制代码
package com.pb.demo.demo1;public class Demo {    public static void main(String[] args) {                try {            func();            System.out.println("A");        } catch (Exception e) {            //e.printStackTrace();            System.out.println("C");        }        System.out.println("D");    }        /**     * 抛出异常     * @throws Exception     */    public static void func() throws Exception{        try {            throw new Exception();        } finally {            System.out.println("B");        }    }}
复制代码

 

结果:B,C,D

 

二、对象的实例化过程

复制代码
package com.pb.demo.demo2;public class Test {    public Test(){        System.out.println("Test");    }    }package com.pb.demo.demo2;/** * 继承父类test * @author Administrator * */public class Demo extends Test {        public Demo(){        //这里隐藏了super()        System.out.println("Demo");    }    public static void main(String[] args) {        new Demo();        new Test();    }}
复制代码

结果:

Test
Demo
Test

三、接口

复制代码
package com.pb.demo.demo2;public interface A {}package com.pb.demo.demo2;public class B implements A {      public String func(){      return "func";  }    public static void main(String[] args) {    A a=new B();    a.func();   //编译失败,因为A接口中并没有这个方法}}
复制代码

四、方法重写

复制代码
package com.pb.demo.demo3;public class Fu {    public boolean show(char a){        System.out.println(a);        return true;    }}package com.pb.demo.demo3;public class Demo extends Fu {    /**     * 重写父类的方法     * 调用时会调用子类重写后的方法     */    public boolean show(char a){        System.out.println(a);        return false;    }    public static void main(String[] args) {        int i=0;        Fu f=new Demo();        Demo d=new Demo();        for(f.show('A');f.show('B')&&(i<2);f.show('C')){            i++;            d.show('D');        }        //结果:A,B,    }}
复制代码

结果;A,B

五、接口

复制代码
package com.pb.demo.demo4;public interface A {}package com.pb.demo.demo4;public class B implements A {    public String test(){        return "yes";    }}package com.pb.demo.demo4;public class Demo {    static A get(){        return new B();    }    public static void main(String[] args) {        A a=Demo.get();        System.out.println(a.test()); //编译失败 接口中没有这个方法,这个方法是子类中特有的    }}
复制代码

六、对象初始化

复制代码
package com.pb.demo.demo5;public class Super {    int i=0;    public Super(String a){        System.out.println("A");        i=1;    }    public Super(){        System.out.println("B");        i+=2;    }}package com.pb.demo.demo5;public class Demo extends Super {    public Demo(String a){        System.out.println("C");        i=5;    }    public static void main(String[] args) {        int i=4;        Super d=new Demo("A");        System.out.println(d.i);    }    //B,C,5    }
复制代码

结果:B,C,5

七、匿名内部类的使用

复制代码
package com.pb.demo.demo6;public interface Inter {    public void show(int a,int b);    public void func();}package com.pb.demo.demo6;public class Demo {    public static void main(String[] args) {        // 补足代码,调用两个函数,用匿名内部类实现        Inter inter = new Inter() {            @Override            public void show(int a, int b) {            }            @Override            public void func() {            }        };        inter.show(3, 5);        inter.func();    }}
复制代码

八、

复制代码
package com.pb.demo.demo7;public class TD {    int y = 6;     class Inner {        static final int y = 3;        // 内部类中如果有static成员,这个内部类必须是static的,        void show() {            System.out.println(y); //只能调用局部中final修饰的        }    }}package com.pb.demo.demo7;public class TC {    public static void main(String[] args) {        TD.Inner ti=new TD().new Inner();        ti.show(); //3    }}
复制代码

非静态内部类,不能定义静态成员,

内部类如果有静态成员,则这个内部类必须是静态内部类

九、方法重写和方法重载

复制代码
public class Demo {    int show(int a,int b){        return 0;    }        }/**     * 下列哪里方法可以存在Demo的子类中     */    public int show(int a,int b){        return 0;    }  //可以方法重写       private int show(int a,int b){        return 0;    }  //权限不够   private int show(int a,long b){       return 0;   }   //可以,和父类不是同一个方法,方法重载   public short show(int a,int b){       return 0;   }//不可以,不是方法重载,不可以和给定的函数出现在同一类中,或者其子类      public static int show(int a,int b){       return 0;   }   //不可以,静态方法,只能覆盖静态
复制代码

 

十、this,final

this:代表本类对象,哪个对象调用this所有的函数,this就代表哪个对象

final: 

1.修饰类,变量(成员变量,静态变量,局部变量),函数

2.修饰的类不可以被继承

3.修饰的函数不可以被重写

4.修饰的变量是一个常量,只能赋值一次

5.内部类只能访问局部中的final变量

十一、继承对象初始化

复制代码
package com.pb.demo.demo8;public class Fu {    int num=4;    void show(){        System.out.println("show Fu");    }}package com.pb.demo.demo8;public class Zi extends Fu {    int num=5;    void show(){        System.out.println("show Zi");    }}package com.pb.demo.demo8;public class T { public static void main(String[] args) {    Fu f=new Zi();    Zi z=new Zi();    System.out.println(f.num);//4    System.out.println(z.num);//5    f.show(); //方法已经重写,执行子类的方法    z.show();}}
复制代码

结果:

4
5
show Zi
show Zi

 

十二、接口的实现

复制代码
package com.pb.demo.demo8;public interface A {    void show();}package com.pb.demo.demo8;public interface B {        void add(int a,int b);}package com.pb.demo.demo7;import com.pb.demo.demo8.A;import com.pb.demo.demo8.B;public class C implements A,B{        private int x;    private int y;    // private int sum;    @Override    public void add(int a, int b) {        this.x=a;        this.y=b;                //sum=a+b;    }    @Override    public void show() {        System.out.println(x+y);        //System.out.println(sum);            }    public static void main(String[] args) {        C c=new C();        c.add(4, 2);        c.show();//通过函数打印以上2个数的和    }}
复制代码

十三、异常执行

复制代码
package com.pb.demo.demo8;public class Demo {    public static void main(String[] args) {         try {            showExce();            System.out.println("A");        } catch (Exception e) {            System.out.println("B");        }finally{            System.out.println("C");        }         System.out.println("D");         //B,C,D    }    public static void showExce() throws Exception{        throw new Exception();    }}
复制代码

结果:B,C,D

 

十四、继承,子类重写父类

复制代码
package com.pb.demo.demo8;public class Super {    int i=0;    Super(){            };    public Super(String s){        i=1;    }    }package com.pb.demo.demo8;public class Demo1 extends Super {    public Demo1(String s){        i=2;    }        public static void main(String[] args) {        Demo1 d=new Demo1("yes");        System.out.println(d.i); //2    }}
复制代码

结果:2

十五、方法重写

 

复制代码
package com.pb.demo.demo9;public class Super {    public int get(){        return 4;    }}package com.pb.demo.demo9;public class Demo1 extends Super {        public long get(){ //方法重写不能改变返回值类型和参数        return 4;    }    public static void main(String[] args) {        Super s=new Demo1();        System.out.println(s.get());    }}
复制代码

 

 

结果:编译失败:方法重写不能改变方法的返回值 和参数

十六、异常

 

复制代码
package com.day10.demo2;public class Demo {    public static void main(String[] args) {        try {             func();        } catch (Exception e) {            System.out.println("C");                    }        System.out.println("D");            }    public static void func(){        try {            throw new Exception();            System.out.println("A");//抛出异常下方代码不会被执行        } catch (Exception e) {            System.out.println("B");                    }    }}
复制代码

 

结果:throw下方的代码不能被执行到

删除后,执行结果:B,D

十七、匿名内部类

 

复制代码
package com.day10.demo2;public class Demo1 {        public void func(){        //位置1:            }        public static void main(String[] args) {        Demo d=new Demo();        //位置2            }    /**     * A、在位置1写new Inner(); //OK     * B、在位置2写new Inner(); //主函数是静态的,如果要访问,需要被静态包修     * C、在位置2写new d.Inner();//格式错误 new new Demo1().Inner();     * D、在位置2写new Demo.Inner(); //错误因为inner不是静态的     */}
复制代码

 

结果

十八、多重catch顺序

 

复制代码
package com.day10.demo2;public class Exc0 extends Exception {}package com.day10.demo2;public class Demo2 {    public static void main(String[] args) {        try {            throw new Exc1();        } catch (Exception e) {            System.out.println("Exception");            /*             * Exception是最大的异常父类,             * 在最前面的后,下面的catch永远不会被执行到             */        }catch (Exc0 e) {            System.out.println("Exc0");        }    }}
复制代码

 

结果:在最前面的后,下面的catch永远不会被执行到,父类的catch要放在最下面

十九、静态调用非静态,匿名内部类实现

 

复制代码
package com.day10.demo2;public interface Test {        public void func();}
复制代码

 

复制代码
package com.day10.demo2;public class Demo3 {    public  void show(Test t){        t.func();    }    public static void main(String[] args) {        //补足代码:匿名内部类        new Demo3().show(new Test(){            @Override            public void func() {                            }                    });            }}
复制代码

 

二十、异常带return

 

复制代码
package com.day10.demo3;public class Test {    public static String output="";    public static void foo(int i){        try {            if(i==1)                throw new Exception();                    output+="1";        } catch (Exception e) {            output+="2";            return;        }finally{            output+="3";                    }        output+="4";    }        public static void main(String[] args) {                foo(0);        System.out.println(output);//1,3,4        foo(1);        System.out.println(output); //134,2,3    }}
复制代码

 

结果

二十一、补全代码对象数组求最大值

 

复制代码
package com.day10.demo3;public class Circle {    private static double pi=3.14;    private double radius;//半径    public Circle(double r){        this.radius=r;    }    public static double compare(Circle[] cir){        //程序代码,就是求数组中的最大值        int max=0;        for(int x=1;x<cir.length;x++){            if(cir[x].getRadius()>cir[max].radius){                max=x;            }                    }        return cir[max].getRadius();            }    public double getRadius() {        return radius;    }    public void setRadius(double radius) {        this.radius = radius;    }}package com.day10.demo3;public class TC {    public static void main(String[] args) {                Circle cir[] =new Circle[3];//创建了个类类型的数组        cir[0]=new Circle(2.0);        cir[1]=new Circle(3.0);        cir[2]=new Circle(5.0);        System.out.println("最大半径值是:"+Circle.compare(cir));    }}
复制代码

 

二十二、方法调用

 

复制代码
package com.day10.demo3;public class Demo {    private static int j = 0;    private static boolean methodB(int k) {        j += k;        return true;    }    public static void methodA(int i) {        boolean b;        b = i < 10 | methodB(4);        b = i < 10 ||methodB(8);    }    public static void main(String[] args) {        methodA(0);        System.out.println(j);    }}
复制代码

结果:4

二十三、对象中值的比较

 

 

复制代码
package com.day10.demo4;public class Circle {    private double radius;    public Circle(double r){        this.radius=r;    }    public Circle compare(Circle cir){        //补足代码        if (this.radius>cir.getRadius()){            return this;        }else{            return cir;        }        //return (this.radius>cir.getRadius()?this:cir);    }    public double getRadius() {        return radius;    }    public void setRadius(double radius) {        this.radius = radius;    }    }package com.day10.demo4;public class TC {    public static void main(String[] args) {        Circle cir1=new Circle(1.0);        Circle cir2=new Circle(2.0);        Circle cir;        cir=cir1.compare(cir2);        if(cir1==cir){            System.out.println("圆1的半径比较大");        }else{            System.out.println("圆2的半径比较大");        }    }}
复制代码

 

 

0 0
原创粉丝点击