基础篇 _练习 小练习1~4

/*
 小练习1:写出程序结果
 */
package com.test4;

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

public class lianxi_2 extends Test
{
	lianxi_2()
	{
		System.out.println("空");
	}
	lianxi_2(String s)
	{
		//super(s);//这是隐式构造函数
		System.out.println("lianxi_2");
	}
	
	public static void main(String[] args) 
	{
		new lianxi_2();
		//new Test("a");
	}

}

结果:



/*
 	小练习2:写出程序结果
 */
package com.test4;

interface A
{}
class B implements A 
{
	public String func()
	{
		return "func";
	}
}
public class lianxi {

	public static void main(String[] args) {
		
		A a=new B();
		System.out.println(a.func());
	}

}




//运行结果:编译失败,A中没有定义func方法。


/*
 小练习3:补足代码
 */
package com.test4;

interface Inter
{
	void show(int a,int b);
	void func();
}

public class lianxi_4 {

	public static void main(String[] args)
	{
		//补足代码,使用匿名内部类调用两个方法
		
	}
	

}

答案:

interface Inter
{
	void show(int a,int b);
	void func();
}

public class lianxi_4 {

	public static void main(String[] args)
	{
		//补足代码,使用匿名内部类调用两个方法
		<span style="color:#ff6666;">DiaoYong().show(1, 3);
		DiaoYong().func();
	}
	
	static Inter DiaoYong()
	{
		return new Inter()
			{
				public void show(int a,int b)
				{
					System.out.println("show run");
				}

				public void func() 
				{
					System.out.println("func run");
				}
			};
	}
</span>
}

还可以这样:

interface Inter2
{
	void show(int a,int b);
	void func();
}

public class lianxi_5 {

	public static void main(String[] args) {
		
		//补足代码,使用匿名内部类调用两个方法
		<span style="color:#ff6666;">Inter2 in=new Inter2()
		{
			public void show(int a,int b)
			{
				System.out.println("show run");
			}
			public void func()
			{
				System.out.println("func run");
			}
		};
		
		in.show(1, 2);
		in.func();</span>
	}

}


/*
 练习4
 */
package com.test5;

class Fu
{
	int show(int a,int b){ return 0; }
	
}
class Zi extends Fu
{
	/*能编译通过吗?不能请写出理由。
	A.public int show(int a,int b){ return 0; }//可以,覆盖
	B.private int show(int a,int b){ return 0; }//不可以,权限不够
	C.private int show(int a,long b){ return 0; }//可以,不是一个函数,相当重载
	D.public short show(int a,int b){ return 0; }//不可以,不能和父类show函数同时出现
<span style="white-space:pre">							</span> 在同一个类中,或者子父类中。
	E.static int show(int a,int b){ return 0; }//不可以。静态方法只能覆盖静态方法。
	  
	*/
	
}

public class lianxi_6 {

	public static void main(String[] args) {
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值