Day10练习

A:简答题

1.形式参数是基本类型要的是什么?是类名要的是什么?是抽象类名要的是什么?是接口名要的是什么?
	基本类型:基本数据
	类名:该类对象
	抽象类名:抽象类子类对象
	接口名:实现接口的子类对象
2.返回值类型是基本类型返回的是什么?是类名返回的是什么?是抽象类名返回的是什么?是接口名返回的是什么?
	基本类型:基本数据类型
	类名:该类对象
	抽象类名:抽象类子类对象
	接口名:实现接口的子类对象
3.package它有什么作用?
	解决一个路径下不能存在同名文件问题
	根据功能进行区分
4.import的作用?
	引入包
5.内部类有哪些访问特点?
	内部类可以直接访问外部类的所有成员
	外部类访问内部类成员 需要创建对象
6.匿名内部类的格式是什么?其本质是什么?
	new 类名(){
		重写方法;
	};
	本质:继承了该类的或者实现了该接口的子类匿名对象

B:看程序写结果(写出自己的分析理由),程序填空,改错,看程序写结果。

1、请补全下列代码,完成可以访问到内部类InnerClass?

class Outer {
    class Inner {
    }
}
class InnerClassDemo {
    public static void main(String[] args) {
        ___Outer.Inner inner = new Outer().new Inner();___
    }
}
/*
在第七行添加
Outer.Inner inner = new Outer().new Inner();
*/

2、给出以下代码,请问该程序的运行结果是什么?

class Outer {
    String s = "Outer";
    public static void main(String[] args) {
        new Outer().new Inner();
    }
    Outer() {
        System.out.println(s);
    }
    class Inner {
        String s = "Inner";
        Inner() {
            System.out.println(s);
        }
    }
}
/*
Outer
Inner
*/

3、给出以下代码,请问该程序的运行结果是什么?

final class Outer {
    class Inner {
        void method() {
            if (Outer.this.flag) {
                function();
            }
        }
    }
    private boolean flag = true;
    public void function () {
        System.out.println("Function");
    }
    public Outer() {
        new Inner().method();
    }
    public static void main (String[] args) {
        new Outer();
    }
}
/*
Function
*/

4、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。

class Outer {
    final String s = "我是外部类变量";
    public void method () {
        String s1 = "我是内部类变量";
         final class Inner {
            public void innerMethod() {
                int x = 20;
                System.out.println(s);
                System.out.println(x);
                System.out.println(s1);
            }
        }
        __Inner inner = new Inner();__
        __inner.innerMethod();__
    }
}
__public class Test{__
    public static void main(String[] args){
        new Outer().method();
    }
__}__
/*
第13,14,17,21行 需要自己添加
我是外部类变量
20
我是内部类变量
*/

5、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。

class SuperEx {
    String s = "父类成员变量";
    public void method(){
        System.out.println("SupMethod");
    }
}
class SubEx extends SuperEx {
    String s = "子类成员变量";
    public void method(){
        System.out.println("SubMethod");
    }
}
class Demo {
    public static void main (String[] args) {
        SubEx sub = new SubEx();
        SuperEx sup = new SubEx();
        System.out.println(sub.s);
        function( sub );
        System.out.println(sup.s);
        function( sup );
    }
    public static void function (SuperEx sup) {
        sup.method();
    }
}
/*
子类成员变量
SubMethod
父类成员变量
SubMethod
*/

6、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。

abstract class SuperEx {
    String s = "父类成员变量";
    public abstract void method();
}
class SubEx extends SuperEx {
    String s = "子类成员变量";
    public void method(){
        System.out.println("SubMethod");
    }
}
class Demo {
    public static void main (String[] args) {
        SubEx sub = new SubEx();
        SuperEx sup = new SubEx();

        System.out.println(sub.s);
        function( sub );
        System.out.println(sup.s);
        function( sup );
    }
    public static void function (SuperEx sup) {
        sup.method();
    }
}
/*
子类成员变量
SubMethod
父类成员变量
SubMethod
*/

7、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。

interface Inter {
    public static final String s = "接口常量";
    public abstract void method();
}
class SubEx implements Inter {
    String s = "子类成员变量";
    public void method(){
        System.out.println("SubMethod");
    }
}
class Demo {
    public static void main (String[] args) {
        SubEx sub = new SubEx();
        Inter in= new SubEx();

        System.out.println(sub.s);
        function( sub );
        System.out.println(in.s);
        function( in );
    }
    public static void function (Inter in) {
        in.method();
    }
}
/*
子类成员变量
SubMethod
接口常量
SubMethod
*/

8、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。

class SuperEx {
    String s = "父类成员变量";
    public void method(){
        System.out.println("SupMethod");
    }
}
class SubEx extends SuperEx {
    String s = "子类成员变量";
    public void method(){
        System.out.println("SubMethod");
    }
}
class Demo {
    public static void main (String[] args) {
        SuperEx sup = getInstance();
        System.out.println(sup.s);
        function( sup );
    }
    public static SuperEx getInstance(){
        return new SubEx ();
    }
    public static void function (SuperEx sup) {
        sup.method();
    }
}
/*
父类成员变量
SubMethod
*/

9、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。

package org;

abstract class SuperEx {
    String s = "父类成员变量";
    public void method();
}
class SubEx extends SuperEx {
    String s = "子类成员变量";
    public void method(){
        System.out.println("SubMethod");
    }
}
class Demo {
    public static void main (String[] args) {
        SuperEx sup = getInstance();
        System.out.println(sup.s);
        function( sup );
    }
    public static SuperEx getInstance(){
        return new SubEx ();
    }
    public static void function (SuperEx sup) {
        sup.method();
    }
}
/*
第5行改为public abstract void method();
	或public void method(){}
父类成员变量
SubMethod
*/

10、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。

interface Inter {
    public static final String s = "接口常量";
    public abstract void method();
}
class SubEx implements Inter {
    String s = "子类成员变量";
    public void method(){
        System.out.println("SubMethod");
    }
}
class Demo {
    public static void main (String[] args) {
        Inter in = getInstance();
        System.out.println(in.s);
        function( in);
    }
    public static Inter getInstance(){
        return new SubEx ();
    }
    public static void function (Inter in) {
        in.method();
    }
}
/*
接口常量
SubMethod
*/

C:编程题

1.请编写程序,通过接口的方式创建匿名内部类

interface Inter {
    public abstract void show();
}
//在外部类Outer的method()方法中,创建匿名内部类
public class Homework01 {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.method();
    }
}
interface Inter {
    public abstract void show();
}
class Outer{
    public void method(){
        Inter inter = new Inter(){
            @Override
            public void show() {
                System.out.println("匿名内部类");
            }
        };
        inter.show();
    }
}
/*
匿名内部类
*/

2.请编写程序,通过抽象类的方式创建匿名内部类

abstract class AbstractClass {
	public abstract void show();
}
//在外部类Outer的method()方法中,创建匿名内部类
public class Homework02 {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.method();
    }
}
abstract class AbstractClass {
    public abstract void show();
}
class Outer{
    public void method(){
        AbstractClass abstractClass = new AbstractClass() {
            @Override
            public void show() {
                System.out.println("匿名内部类");
            }
        };
        abstractClass.show();
    }
}
/*
匿名内部类
*/

3.请编写一个类中定义了一个接口然后在该类中在使用这个接口.

public class Homework03 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();
    }
}
interface Animal{
    void eat();
}
class Dog {
    public void eat(){
        Animal animal = new Animal() {
            @Override
            public void eat() {
                System.out.println("狗吃骨头");
            }
        };
        animal.eat();
    }
}
/*
狗吃骨头
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值