4.4.3Object类

习速度节奏稍微有点慢,先来看代码实现:

/*------------------------这里说如果方法参数被定义成一个接口,则实现就需要定义一个类实现接口。除此之外我们还可以实现匿名内部类实现此操作*/

 interface Animal {
  void shout();
}
public class Example{
 public static void main(String[] args) {  
  animalShout(new Animal() {        //匿名内部类,修改接口实现类
   @Override
   public void shout() {
    // TODO 自动生成的方法存根
    System.out.println("汪汪!");
   }
  });
  
 }
 public static void animalShout(Animal an) {
  an.shout();
 }
 
 
}
/*------------------------这里说如果方法参数被定义成一个接口,则实现就需要定义一个类实现接口。*/
interface Animal {  //定义一个Animal类接口,抽象shout方法
  void shout();
}
class Dog implements Animal{  //专门一个类实现Animal接口,shout方法
 public void shout() {
  System.out.println("汪汪!");
 }
}
 
public class Example{     
 public static void main(String[] args) {  
  
 Example.animalShout(new Dog());   
 
 } 
 public static void animalShout(Animal an) {
  an.shout();
 }
 
}
 
/*------------------------以上是实现过程的两种代码,现将格式给出-----------------------*/
new  父类(参数列表)或父接口(){
//匿名内部实现部分
}
 
 
 
原文地址:https://www.cnblogs.com/Wision-22/p/9876444.html