Java Core Technology (5): Interface and Internal Class

1. Interface

  • There can only be constant fields and unimplemented methods in the interface. It cannot include instance domains, static methods, and method implementations. The task of providing instance domains and method implementations should be completed by the class that implements the interface.
  • Some interfaces only define constants, but not methods. But it is better not to use the interface like this.
  • All methods in the interface are automatically public, and the fields in the interface will be automatically set to public static final.
  • You can think of an interface as an abstract class without an instance domain, but there are still some differences between the two concepts.
  • The biggest difference between an interface and an abstract class is that an interface can be inherited multiple times, while all classes, including abstract classes, can only be inherited in a single way.

1. Steps to implement the interface

  In order for a class to implement an interface, the following two steps are usually required:

  • Declare the class to implement the given interface.
  • Implement all methods in the interface.
	class Employee implements Comparable{
    
    //将类声明为实现给定的接口
	
		public int compareTo(Object otherObject){
    
    //实现接口中的方法
			…………
		}
	}

  It should be noted that the method in the interface is not decorated with keywords, but the default is public; in the implementation class, the method must be decorated with public, because if you do not use any keyword modification, the package is visible by default. In short, the visibility of the implementation class cannot be lower than that of the abstract class, and the visibility of the child class cannot be lower than that of the parent class.

  It is necessary to consider generics:

	class Employee implements Comparable<Employee>{
    
    //将类声明为实现给定的接口
	
		public int compareTo(Employee otherObject){
    
    //实现接口中的方法
			…………
		}
	}

2. Interface characteristics

  • Interface is not a class, especially you cannot use the new operator to instantiate an interface:
	x = new Comparable(..); // ERROR
  • However, although the object of the interface cannot be constructed, the variables of the interface can be declared:
	Comparable x; // 0K
  • The interface variable must refer to the class object that implements the interface.
  • Next, just like using instanceof to check whether an object belongs to a certain class, you can also use instance to check whether an object implements a certain interface:
	if (an0bject instanceof Comparable){
    
     ...}
  • Interfaces can be inherited, and one interface can inherit multiple interfaces.

Two, object cloning

  Since the copy of an object can only copy the reference, we use the clone() method to copy the value pointed to by the object.

  However, the default cloning operation is shallow copy, which does not clone the internal objects contained in the object. In other words, if the point of the object is still the reference object, its reference is copied.

Insert picture description here

  What happens if you make a shallow copy? It depends on the specific situation. If the child objects shared by the original object and the shallow cloned object are immutable, there will be no problem. But the more common situation is that the sub-object is mutable, so the clone method must be redefined in order to achieve a deep copy of the cloned sub-object.

  Even if the default implementation of clone (shallow copy) can meet the requirements, it should implement the Cloneable interface, redefine clone as public, and call super.clone( ). Here is an example:

	class Employee inplements Cloneable
	{
    
    
		……
		public Employee clone() throws CloneNotSupportedException //如果在方法中没有实现clone方法,就会抛出异常
		{
    
    
			Employee cloned = (Employee)super.clone();
			cloned.hireDay=(Date)hireDay.clone();
			return cloned;
		}
	}	

  The clone method is somewhat cumbersome, and we should avoid using it.

Three, internal class

  An inner class is a class defined in another class.

1. Conventional internal class

  • The inner class can access its own data field as well as the data field of the outer class object that created it.
  • The inner class is declared as private, so that only its outer class can access it. Only inner classes can be private classes, while regular classes can only have package visibility or public visibility.
  • How to use external class references in classes: external class name.this
  • Reference of external class and internal class:
	OuterClass o1=new OuterClass();
	OuterClass.InnerClass o2= o1.new InnerClass();

2. Local inner class

  • You can define a local inner class in a method of a class.
  • Local inner classes can access method parameters, but the parameters must be declared as final.
	class Outer
	{
    
    
		public void start(int i,final int j)//内部类能访问j,但无论如何j是不可以修改的
		{
    
    
			class Inner
			{
    
    
				public int getJ()
				{
    
    
					return j;
				}
			}
		}
	}

3. Anonymous inner class

  Taking the use of local inner classes one step further, if you only create an object of this class, you don't need to name it. This kind of class is called an anonymous inner class.

	public void start(int interval,final boolean beep)
	{
    
    
		//匿名内部类的实现,不一定是要实现一个接口,也可以是一个类,在花括号中需要对该类进行扩展
		ActionListener listener = new ActionListener()
		{
    
    
			public void ationPerformed(ActionEvent event){
    
    //由于ActionListener是一个接口,需要实现接口的方法
				……
			}
		}
	}

4. Static inner class

  Sometimes, the use of inner classes is just to hide one class inside another class, and there is no need for the inner class to refer to the outer class object. To this end, you can declare the inner class as static in order to cancel the generated reference.

  An object of a static inner class is exactly the same as all other inner classes except that it has no reference privileges to the outer class object that generated it.

  If a class object is constructed in a static method, the corresponding class can only be a static inner class. Only inner classes can be declared as static.

Guess you like

Origin blog.csdn.net/Tracycoder/article/details/112341566