1.接口
接口(interface)是抽象方法和常量值定义的集合。
接口是实现功能,比如,鼠标、键盘、打印机他们都有usb口,这是他们共同的功能,三者之间并不存在子父类的关系

2.代理模式设计模式
interface A {
int x = 0;
}
class B {
int x = 1;
}
class C extends B implements A {
public void pX() {
System.out.println(x);
}
public static void main(String[] args) {
new C().pX();
} }
有问题,编译不通过。
因为x是不明确的,此时父类和接口对于子类来说是同一条线上的。
开发中尽量不要让属性重名。我们在定义属性的时候尽可能的将名字区分开,不要定义重名的
//正确写法
interface A{
int x=0;
}
class B{
int x=1;
}
class C extends B implements A{
public void pX() {
System.out.orintln(super.x);//调用父类的x
//利用接口中的x是全局常量
System.out.println(A.x);//调用接口中的x
}
public static void main(String[] args) {
new C().pX();
}
}
3.内部类
class Outer {
private int s;//成员变量
public class Inner {//内部类
public void mb() {//内部类的方法
s = 100;
System.out.println("在内部类Inner中s=" + s);
}
}
public void ma() {//外部类的方法
Inner i = new Inner();//创建一个内部类对象
i.mb();//调用内部类的方法
}
}
public class InnerTest {
public static void main(String args[]) {
Outer o = new Outer();//创建一个外部类对象
o.ma();//调用外部类的方法
}
}
2.局部内部类
public class Test2 {
public void method(){//方法
class A{//方法中的局部类
int age;//局部类的成员变量
public int getAge(){//局部类的方法
return age;
}
}
}
}
3.匿名内部类
interface A{
public abstract void fun1();
}
public class Outer{
public static void main(String[] args) {
new Outer().callInner(new A(){
//接口是不能new但此处比较特殊是子类对象实现接口,只不过没有为对象取名
public void fun1() {
System.out.println(“implement for fun1");
}
});// 两步写成一步了
}
public void callInner(A a) {
a.fun1();
}
}
4.面向对象总结

715



被折叠的 条评论
为什么被折叠?



