Anonymous Class

class Machine0 {public void start() {System.out.println("Machine starting");}}interface Plant {void growing();}public class App2 {public static void main(String[] args) {Machine0 machine1 = new Machine0() {@Overridepublic void start() {System.out.println("Camera starting");}};//真正指向的对象是等号后面的有重载method的类Plant plant1 = new Plant() {public void growing() {System.out.println("growing");}};//interfacce本身不能定义对象,但是可以通过后面的真正的类创建一个对象machine1.start();plant1.growing();}}匿名类只能创建一个对象,不能再次实用该类

猜你喜欢

转载自blog.csdn.net/juttajry/article/details/48866771
今日推荐