java开发 关于接口和内部类问题(本人是个菜鸟,刚接触java)
定义一个乐器(Instrument)接口,其中有抽象方法voidplay();在InstrumentTest类中,定义一个方法voidplayInstrument(Ins...
定义一个乐器(Instrument)接口,其中有抽象方法void play();
在InstrumentTest类中,定义一个方法void playInstrument(Instrument ins);并在该类的main方法中调用该方法。要求:分别使用下列内部类完成此题。
1.成员内部类
2.局部内部类
3.匿名类 展开
在InstrumentTest类中,定义一个方法void playInstrument(Instrument ins);并在该类的main方法中调用该方法。要求:分别使用下列内部类完成此题。
1.成员内部类
2.局部内部类
3.匿名类 展开
2个回答
展开全部
成员内部类实现
package Demo5;
public class InstrumentTest {
/**
* 钢琴成员内部类,实现Instrument接口
* @author Administrator
*
*/
class Piano implements Instrument{
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("演奏钢琴");
}
}
/**
* 小提琴类,同样实现接口Instrument
* @author Administrator
*
*/
class Violin implements Instrument{
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("演奏小提琴");
}
}
void playInstrument(Instrument ins){
ins.play();
}
public static void main(String[] args){
/**
* 创建外部类的对象
*/
InstrumentTest test = new InstrumentTest();
Instrument piano = test.new Piano();
piano.play();
Instrument violin = test.new Violin();
violin.play();
}
}
局部内部类
package Demo5;
public class InstrumentTest {
public void show(){
class Piano implements Instrument{
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("演奏钢琴");
}
}
class Violin implements Instrument{
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("演奏小提琴");
}
}
Instrument piano = new Piano();
piano.play();
Instrument violin = new Violin();
violin.play();
}
public static void main(String[] args){
InstrumentTest test = new InstrumentTest();
test.show();
}
}
匿名内部类
package Demo5;
public class InstrumentTest {
public Instrument show(){
return new Instrument(){
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("演奏~~");
}
};
}
public static void main(String[] args){
InstrumentTest test = new InstrumentTest();
test.show().play();
}
}
package Demo5;
public class InstrumentTest {
/**
* 钢琴成员内部类,实现Instrument接口
* @author Administrator
*
*/
class Piano implements Instrument{
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("演奏钢琴");
}
}
/**
* 小提琴类,同样实现接口Instrument
* @author Administrator
*
*/
class Violin implements Instrument{
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("演奏小提琴");
}
}
void playInstrument(Instrument ins){
ins.play();
}
public static void main(String[] args){
/**
* 创建外部类的对象
*/
InstrumentTest test = new InstrumentTest();
Instrument piano = test.new Piano();
piano.play();
Instrument violin = test.new Violin();
violin.play();
}
}
局部内部类
package Demo5;
public class InstrumentTest {
public void show(){
class Piano implements Instrument{
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("演奏钢琴");
}
}
class Violin implements Instrument{
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("演奏小提琴");
}
}
Instrument piano = new Piano();
piano.play();
Instrument violin = new Violin();
violin.play();
}
public static void main(String[] args){
InstrumentTest test = new InstrumentTest();
test.show();
}
}
匿名内部类
package Demo5;
public class InstrumentTest {
public Instrument show(){
return new Instrument(){
@Override
public void play() {
// TODO Auto-generated method stub
System.out.println("演奏~~");
}
};
}
public static void main(String[] args){
InstrumentTest test = new InstrumentTest();
test.show().play();
}
}
追问
还有匿名内部类呢?这个要怎么写?
追答
其实我也是初学者 我昨天才看到这里 正好我有一个ppt你要不要
展开全部
成员内部类:
public class InstrumentTest{
class ChildInstrument extends Instrument
{
void play(){
具体实现
}
}
public static void main(String[] args){
ChildInstrument c = new ChildInstrument();
c.play();
}
}
局部内部类:(所谓的局部内部类应该是指方法内的类吧!)
public class InstrumentTest{
public static void main(String[] args){
class ChildInstrument extends Instrument
{
void play(){
具体实现
}
}
ChildInstrument c = new ChildInstrument();
c.play();
}
匿名类:
public class InstrumentTest{
public Instrument i= new Instrument(){
void play(){
具体实现。
}
}
public static void main(String[] args){
i.play();
}
}
public class InstrumentTest{
class ChildInstrument extends Instrument
{
void play(){
具体实现
}
}
public static void main(String[] args){
ChildInstrument c = new ChildInstrument();
c.play();
}
}
局部内部类:(所谓的局部内部类应该是指方法内的类吧!)
public class InstrumentTest{
public static void main(String[] args){
class ChildInstrument extends Instrument
{
void play(){
具体实现
}
}
ChildInstrument c = new ChildInstrument();
c.play();
}
匿名类:
public class InstrumentTest{
public Instrument i= new Instrument(){
void play(){
具体实现。
}
}
public static void main(String[] args){
i.play();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询