- 我勒个去,不要小看这一天的内容啊,真特么多,看来我Java继承还真挺通常的,还要继续努力!
- 明天复习这10天内容。
javac -d . test.java
这样编译后生成的class文件路径将是com/heima/test.class
。.
表明当前目录,这里也能够改为别的目录。package com.heima; public class test { }
package com.heima; import com.baidu.Person; class Test { public static void main(String[] args) { Person a = new Person(); System.out.println(a.getName()); } }
package com.baidu; class Person { private String name; public String getName(){ return name; } }
*
通配符开发通常不用,由于它须要检索,挨个匹配,下降效率。本类 同一个包下(子类和无关类) 不一样包下(子类) 不一样包下(无关类) private Y 默认(default) Y Y protected Y Y Y public Y Y Y Y
成员方法:java
外部类要访问内部类的成员,必须建立对象。程序员
外部类.内部类 p = 外部类对象.内部类对象
package test_heima_08; public class Test_Demo07 { public static void main(String[] args){ Outer.Inner p = new Outer().new Inner(); p.test(); } } class Outer { class Inner { public void test(){ System.out.println("Hello"); } } }
在外部类里面建立一个成员方法,而后在该成员方法内建立对象调用内部类的方法。学习
package test_heima; public class Test_Demo08 { public static void main(String[] args){ Outer2 a = new Outer2(); a.kkk(); } } class Outer2 { private int age = 1; private class Inner2 { public void test(){ System.out.println("test"); } } public void kkk(){ Inner2 a = new Inner2(); a.test(); } }
package test_heima; public class Test_Demo09 { public static void main(String[] args){ //外部类.内部类 对象名 = 外部类名.内部类对象 //其实逻辑上应该是 Outer3.Inner oi = Outer3.new Inner(); //可是这样写不符合习惯,全部仍是把new放左边了 Outer3.Inner oi = new Outer3.Inner(); oi.method(); //外部类名.内部类名.方法名 Outer3.Inner.method2(); } } class Outer3 { static class Inner { public void method() { System.out.println("非静态方法调用"); } public static void method2() { System.out.println("静态方法调用"); } } }
package test_heima; public class Test_Demo10 { public static void main(String[] args){ Outer4.Inner oi = new Outer4().new Inner(); oi.show(); } } class Outer4 { public int a = 3; class Inner { public int a = 2; public void show(){ int a = 1; System.out.println(a); System.out.println(this.a); System.out.println(Outer4.this.a); } } }
局部内部类里面的方法调用方式this
package test_heima; public class Test_Demo11 { public static void main(String[] args){ Outer6 p = new Outer6(); p.method(); } } class Outer6 { public void method(){ class Inner { public void test(){ System.out.println("Hello"); } } Inner si = new Inner(); si.test(); } }
package test_heima; public class Test_Demo12 { public static void main(String[] args){ Outer7 a = new Outer7(); a.method(); } } class Outer7 { public void method(){ int a = 1; //在JDK1.7中这样写不行,须要加final,让局部变量进入常量池,保证在方法弹栈后,对象依然能够访问。 class Inner { public void test(){ System.out.println(a); } } Inner si = new Inner(); si.test(); } }
格式:命令行
new 类名或者接口名(){ 重写方法; }
package test_heima; public class Test_Demo13 { public static void main(String[] args){ Outer8 si = new Outer8(); si.method(); } } interface Inter { public void test(); } class Outer8 { class Inner implements Inter { public void test(){ System.out.println("Hello"); } } public void method(){ //如何调用test()? //法1:有名字a // Inner a = new Inner(); // a.test(); //法2:匿名 new Inter(){ public void test(){ System.out.println("Hello"); } }.test(); } }
匿名内部类适用于一次性使用,屡次调用建议使用有名的内部类。code
package test_heima; public class Test_Demo14 { public static void main(String[] args){ Outer11 si = new Outer11(); si.method(); } } interface Inter2 { public void print1(); public void print2(); } class Outer11 { public void method(){ //这样屡次调用使用匿名内部类并不方便,建议屡次调用使用有名的内部类。 new Inter2(){ public void print1(){ System.out.println("print1"); } public void print2(){ System.out.println("print2"); } }.print1(); new Inter2(){ public void print1(){ System.out.println("print1"); } public void print2(){ System.out.println("print2"); } }.print2(); } }
能够将父类引用指向子类对象,可是,因为匿名对象没类名,没法实现向下转型,因此没法使用子类特有的方法。视频
package test_heima; public class Test_Demo14 { public static void main(String[] args){ Outer11 si = new Outer11(); si.method(); } } interface Inter2 { public void print1(); public void print2(); } class Outer11 { public void method(){ Inter2 p = new Inter2(){ //这个匿名内部类其实至关于一个实现了Inter2接口的子类匿名对象 public void print1(){ System.out.println("print1"); } public void print2(){ System.out.println("print2"); } public void print3(){ //这个子类特有的方法无法使用 System.out.println("print3"); } }; p.print1(); p.print2(); //p.print3();//没法使用子类特有方法 } }
package test_heima; //如何调用PersonDemo中的method方法 public class Test_Demo15 { public static void main(String[] args){ //法1:使用多态 // PersonDemo a = new PersonDemo(); // a.method(new Student()); //法2:使用匿名内部类 PersonDemo a = new PersonDemo(); a.method(new Person(){ public void talk(){ System.out.println("Hello1"); } }); } } abstract class Person { public void talk(){} } class PersonDemo { public void method(Person p){ p.talk(); } } //法1:使用多态 class Student extends Person { public void talk(){ System.out.println("Hello"); } }
package test_heima; public class Test_Demo16 { public static void main(String[] args){ Outer33.method().show(); } } interface Inter33 { void show(); } class Outer33 { //补齐代码 public static Inter33 method(){ //返回Inter33接口的子类对象 return new Inter33(){ public void show(){ System.out.println("HelloWorld"); } }; } } //控制台输出"HelloWorld"