Use of java object-oriented 21 interface interface

Posted by y.t. on Fri, 21 Feb 2020 03:46:25 +0100

/*

  • Use of interface s
  • 1. Define an interface using the format of interface interface name {}. The primary purpose of an interface is to be implemented by an implementation class.(interface-oriented programming)
  • 2. Interfaces and classes are side-by-side structures in java.
  • 3, Members inside the interface
  • 3.1 In JDK7 and earlier versions, only global constants and abstract methods can be defined in interfaces
  • Global constants: public static final variable type variable name = value; because the default is a public static constant, which can be omitted before writing

  • Global constants can be invoked using the interface name.variable name.
  • Abstract method: public abstract method name (); can be omitted before writing

  • 3.2 In JDK8 and later versions, static and default methods can be defined within interfaces.
  • 4. Interfaces cannot be instantiated, there cannot be constructors inside
  • 5. Interfaces need to be used by implementing classes
  • 5.1 If the implementation class implements all the abstract methods in the interface, the implementation class can be instantiated.
  • 5.2 If all abstract methods are not overridden, the implementation class is still abstract and cannot be instantiated
  • 6. Implementing classes in java implements multiple interfaces, and disguising implements multiple inheritance functions.
  • The format is class class class name implements interface name 1, interface name 2,...{}
  • If there is a parent, inherit the parent on writing first and implement the interface
  • The format is class class class name extends parent class name implements interface name 1, interface name 2,...{}
  • All abstract methods in an implementation class that implement multiple interfaces are required to be instantiated.
  • Interface abstraction method subclasses implemented in the parent class can be inherited and no longer implemented
  • 7. Interfaces can inherit from one another or from another
  • The format is interface interface name 1 extends interface name 2, interface name 3,...{}
  • Implementation classes need to implement all abstract methods in interfaces and other interfaces inherited by interfaces before they can be instantiated.
  • Inherited interfaces have duplicate methods with the same parameters, and implementation classes only need to implement one method.
  • 8, the specific use of the interface can reflect polymorphism, method parameters can define abstract classes or interfaces, when parameters are passed in
  • Implement class object passing in using a subclass object or interface of an abstract class.
  • 9. Interfaces can be interpreted as a specification that defines a set of rules that represent the real world "if you are/want to...be
  • Must be able to...Thoughts.Inheritance is a yes-no relationship, while interface implementation is a yes-no relationship.
  • 10, like abstract classes, interfaces can also have anonymous implementation classes or anonymous objects.The format is the same as for abstract classes.
  • 11, Application of Interface: Proxy Mode
  • Scenarios:
    *1, Security Agent: Block direct access to real roles._
  • 2, Remote Agent: Handles remote method calls (RMI) through the proxy class
  • 3, Delayed loading: Load lightweight proxy objects first, really need to load real objects such as you want to develop a large document viewing software,
  • There are large pictures in a large document, maybe one picture has 100MB. When opening a file, it is not possible to display all pictures, so you can use a proxy.
  • Mode, use proxy to open large pictures when you need to view them.Felt
  • classification
  • 1, Static Proxy (Statically Defined Proxy Class)
  • 2. Dynamic Proxy (Dynamic Generation of Proxy Classes) Dynamic Proxy that comes with JDK requires reflection and other knowledge

*/

package object_chapter2;

import javax.management.RuntimeErrorException;

public class Object_Interface {
public static void main(String[] args) {
	System.out.println(A.MAX_SPEED);//Global constants in interfaces can be invoked via interface name.property name
	D d = new D();
	d.method();
	new E().method();
	new E().method(10);
	new E().out();
	//Non-Anonymous Implementation Class Non-Anonymous Object Call
	USB m = new MP3();//Polymorphism	
	Computer c = new Computer();
	c.connect(m);
	//Nonanonymous implementation class anonymous object call
	c.connect(new Camera());
	//Anonymous implementation class non-anonymous object call
	USB u = new USB() {

		@Override
		public void transferDate() {
			// TODO Auto-generated method stub
			System.out.println("Mobile Transfer Files");
		}

		@Override
		public void charge() {
			// TODO Auto-generated method stub
			System.out.println("Mobile phone charging");
		}
		};
		c.connect(u);
	//Anonymous implementation class anonymous object call
		c.connect(new USB() {

			@Override
			public void transferDate() {
				// TODO Auto-generated method stub
				System.out.println("Move hard disk to transfer movies");
			}

			@Override
			public void charge() {
				// TODO Auto-generated method stub
				System.out.println("Power Supply to Mobile Hard Disk");
			}});
		//Test Agent Mode
		Designer design = new Customer();
		Designer company = new Company(design);
		company.CAD();
		//Test Comparison Class Implements Comparison Interface Abstract Method
		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("Who am I?");
	}
	
}
class E extends D implements C{
	
//  @Override
//	public void method() {
//		// TODO Auto-generated method stub
//		System.out.println("Where am I");
//	}

	@Override
	public void method(int n) {
		// TODO Auto-generated method stub
		System.out.println("What do I want to do");
		System.out.println(Math.sqrt(n));		
	}		
	
	void out() {
//		e = new E();//e is a global constant and cannot be modified
//		System.out.println(MAX_SPEED); //Compilation failed due to duplicate name
		System.out.println(super.MAX_SPEED);//MAX_SPEED method of invoking parent class
		System.out.println(A.MAX_SPEED);//MAX_SPEED Method of Call Interface
	}
}

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("Transfer music");
	}

	@Override
	public void charge() {
		// TODO Auto-generated method stub
		System.out.println("MP3 Charging");
	}
}
	
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("Transfer photos");
	}

	@Override
	public void charge() {
		// TODO Auto-generated method stub
		System.out.println("Camera charging");
	}
	
}

//proxy pattern

interface Designer{
	void CAD();
}

class Customer implements Designer{

	@Override
	public void CAD() {
		// TODO Auto-generated method stub
		System.out.println("Get design drawings");
	}
	
}

class Company implements Designer{
	private Designer d;
	Company(Designer d){
		this.d = d;
	}
    void professionalDesign() {
    	System.out.println("Designed by professionals");
    }
	@Override
	public void CAD() {
		// TODO Auto-generated method stub
		professionalDesign();
		d.CAD();
	}
	
}

//Create comparison interface

interface CompareObject{
	int compareTo(Object o);
}

//Create implementation classes for comparison interfaces and compare objects for Camera classes.

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()); //You can convert the data to a wrapper class and directly invoke the comparison method of the wrapper class
			
		}else {
			throw new RuntimeException("Data type error");
		}
	}	
	
	public int compareToWeight(Object o) {
		if(this == o) {
			return 0;
		}
		if(o instanceof Camera) {
			Camera cam = (Camera)o;
			return this.weight.compareTo(cam.weight);	//For packaging classes, the comparison methods in packaging classes can be retrieved directly	
		}else {
			throw new RuntimeException("Data type error");
		}
	}
}
Thirty-seven original articles were published, 1 was praised, and 756 were visited
Private letter follow

Topics: Mobile Java Programming JDK