/*
- Use Interface (interface) of
- 1, using the interface name interface format {} define an interface, the interface is the main purpose is to achieve implementation class. (Oriented Programming Interface)
- 2, with the interface in java class structure are juxtaposed,
- 3, inside the interface members
- JDK7 3.1 and previous versions, the interface which can only define global constants and abstract methods
-
Global Constants: public static final variable type variable name = value; because the default public static constant, may be omitted when the writing front
- You can use the interface name. Mode variable name is called global constants.
-
Abstract methods: public abstract method name (); in front of the writing can be omitted
- In JDK8 3.2 and later versions, the interface can be defined inside a static method and the default method.
- 4, the interface can not be instantiated, which can not have constructor
- 5, need to be used by the interface implementation class (the implements)
- If implemented 5.1 abstract class implements all the methods in an interface, the implementation class will be instantiated.
- 5.2 If there is no override all the abstract methods, implementation class is still abstract class can not be instantiated
- 6, java implementation class can implement multiple interfaces, multiple inheritance to achieve a disguised function.
- The format of the class name class implements an interface name, an interface name 2, ...} {
- If you have a parent class, inherited his father's writing on the first class that implements the interface
- Format extends class parent class name for the class name implements an interface name, an interface name 2, ...} {
- All abstract methods class need to implement multiple interfaces can be instantiated.
- Abstract parent class interfaces implemented method subclass can inherit, no longer achieved
- 7, single inheritance between interfaces and interfaces can also be multiple inheritance
- The format of the interface name interface 2 1 extends interface name, the name of the interface 3, ...} {
- All other abstract methods interface implementation class needs to implement the interface with the interface inheritance can only be instantiated.
- Interfaces inherited methods have the same name, the same parameters, to achieve class need only implement a method.
- 8, the interface may be embodied using specific polymorphisms method parameter can be defined in an abstract class or interface, when the incoming parameters
- Objects that implement class or subclass object interface abstract class passed.
- 9, the interface can be understood as a specification defined by a set of rules, it reflects the real world. "If you are / want to ... then
- Must be able to ... "thinking. Inheritance is an" is not "relationship, while the interface is" can "relationship.
- 10, similar to the abstract class interface implementation class may be anonymous or anonymous objects. Abstract class where the same format.
- 11, the application interface: proxy mode
- Application scenarios:
* Ø 1, security agents: direct access to the shielded real role. - 2, the remote agent: invocation (RMI) remote processing method proxy class
- 3, lazy loading: lightweight proxy object to load really need to load the real object For example, you want to develop a large document viewing software,
- There are a large document in the big picture, there may be a picture there is 100MB, when you open the file, impossible to put all the pictures are displayed, so you can use a proxy
- Mode, when you need to see pictures, using proxy to open the big picture.
- classification
- 1, the static agent (statically defined proxy class)
- 2, dynamic proxy (proxy class dynamically generated) JDK own dynamic proxy requires knowledge reflection
- 12, the similarities and differences between abstract classes and interfaces:
- In order to use can not be instantiated, can be inherited, there can have abstract methods, you must have a subclass overrides after: the same point
- difference:
- Definition 1: abstract class is a class abstract method comprising, the interface is a collection of mainly global constants and abstract methods
- Composition 2: abstract class constructor can have, abstract method, ordinary methods, constants, variables. Interfaces may be the global constant, abstract methods, (jdk8.0 increase the default method, static method)
- Use 3: subclass inherits the abstract class (extends), sub-class implements the interface (the implements)
- 4 Relationship: An abstract class can implement multiple interfaces, interfaces can not inherit an abstract class, but allows a plurality of inherited interfaces
- 5 common design patterns: abstract class is a template method, the interface is simple factory, factory method, proxy mode
- 6 objects: all objects instantiated by generating object Polymorphism
- 7 Limitations: abstract class inherits the limitations of a single interface does not have this limitation
- 8 practical application: an abstract class as a template, the interface is expressed as a standard or an ability
- If the abstract classes and interfaces can be used, using an interface priority, because to avoid the limitations of single inheritance
- In development, often see a class is not going to inherit a good class has been achieved, but either inherit the abstract class or implement the interface.
* /
package object_chapter2;
import javax.management.RuntimeErrorException;
public class Object_Interface {
public static void main(String[] args) {
System.out.println(A.MAX_SPEED);//可以通过接口名.属性名的方式调用接口中的全局常量
D d = new D();
d.method();
new E().method();
new E().method(10);
new E().out();
//非匿名实现类非匿名对象调用
USB m = new MP3();//多态性
Computer c = new Computer();
c.connect(m);
//非匿名实现类匿名对象调用
c.connect(new Camera());
//匿名实现类非匿名对象调用
USB u = new USB() {
@Override
public void transferDate() {
// TODO Auto-generated method stub
System.out.println("手机传输文件");
}
@Override
public void charge() {
// TODO Auto-generated method stub
System.out.println("手机充电中");
}
};
c.connect(u);
//匿名实现类匿名对象调用
c.connect(new USB() {
@Override
public void transferDate() {
// TODO Auto-generated method stub
System.out.println("移动硬盘传输电影");
}
@Override
public void charge() {
// TODO Auto-generated method stub
System.out.println("移动硬盘供电中");
}});
//测试代理模式
Designer design = new Customer();
Designer company = new Company(design);
company.CAD();
//测试比较类实现比较接口抽象方法
CompareableCamera c1 = new CompareableCamera(3.2,8);
CompareableCamera c2 = new CompareableCamera(3.3,7);
System.out.println(c1.compareTo(c2));
System.out.println(c2.compareTo(c1));
System.out.println(c2.compareTo(d));
System.out.println(c1.compareToWeight(c2));
System.out.println(c2.compareToWeight(c1));
}
}
interface A{
public static final int MAX_SPEED = 120;
public abstract void method();
}
interface B{
int MIN_SPEED = 80;
void method(int n);
}
interface C extends A,B{
E e = new E();
int MID_SPEED = 100;
void method();
}
class D implements A{
int MAX_SPEED = 160;
@Override
public void method() {
// TODO Auto-generated method stub
System.out.println("我是谁");
}
}
class E extends D implements C{
// @Override
// public void method() {
// // TODO Auto-generated method stub
// System.out.println("我在哪里");
// }
@Override
public void method(int n) {
// TODO Auto-generated method stub
System.out.println("我要干什么");
System.out.println(Math.sqrt(n));
}
void out() {
// e = new E();//e为全局常量,不能修改
// System.out.println(MAX_SPEED);//因为重名,编译不通过
System.out.println(super.MAX_SPEED);//调取父类的MAX_SPEED方式
System.out.println(A.MAX_SPEED);//调取接口的MAX_SPEED方式
}
}
class Computer{
public void connect(USB u) {
u.transferDate();
u.charge();
}
}
interface USB{
void transferDate();
void charge();
}
class MP3 implements USB{
@Override
public void transferDate() {
// TODO Auto-generated method stub
System.out.println("传输音乐");
}
@Override
public void charge() {
// TODO Auto-generated method stub
System.out.println("MP3充电中");
}
}
class Camera implements USB{
private double f;
Integer weight;
public Camera(double f, Integer weight) {
super();
this.f = f;
this.weight = weight;
}
public Camera() {
super();
}
public double getF() {
return f;
}
public void setF(double f) {
this.f = f;
}
@Override
public void transferDate() {
// TODO Auto-generated method stub
System.out.println("传输照片");
}
@Override
public void charge() {
// TODO Auto-generated method stub
System.out.println("相机充电中");
}
}
//代理模式
interface Designer{
void CAD();
}
class Customer implements Designer{
@Override
public void CAD() {
// TODO Auto-generated method stub
System.out.println("得到设计图纸");
}
}
class Company implements Designer{
private Designer d;
Company(Designer d){
this.d = d;
}
void professionalDesign() {
System.out.println("专业人员精心设计");
}
@Override
public void CAD() {
// TODO Auto-generated method stub
professionalDesign();
d.CAD();
}
}
//创建比较接口
interface CompareObject{
int compareTo(Object o);
}
//创建比较接口的实现类,比较Camera类的对象。
class CompareableCamera extends Camera implements CompareObject{
public CompareableCamera() {
super();
// TODO Auto-generated constructor stub
}
public CompareableCamera(double f, Integer weight) {
super(f, weight);
// TODO Auto-generated constructor stub
}
@Override
public int compareTo(Object o) {
if(this == o) {
return 0;
}
if(o instanceof Camera) {
Camera cam = (Camera)o;
if(this.getF() > cam.getF()) {
return 1;
}else if(this.getF() < cam.getF()) {
return -1;
}else {
return 0;
}
// return new Double(this.getF()).compareTo(new Double(cam.getF()));//可以将数据转换为包装类,直接调取包装类的比较方法
}else {
throw new RuntimeException("数据类型错误");
}
}
public int compareToWeight(Object o) {
if(this == o) {
return 0;
}
if(o instanceof Camera) {
Camera cam = (Camera)o;
return this.weight.compareTo(cam.weight); //对于包装类可以直接调取包装类中的比较方法
}else {
throw new RuntimeException("数据类型错误");
}
}
}