Object oriented advanced

inherit

  Inheritance is a cornerstone of java object-oriented programming technology because it allows the creation of hierarchical classes
   Inheritance is that a subclass inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the instance and method of the parent class, or the subclass inherits the method from the parent class, so that the subclass has the same behavior as the parent class
  
  Restrictions on inheritance:
     In java, there is only when inheritance, multiple inheritance, and no multiple inheritance
   Format:
   Class parent class{
  }
  
   Class subclass   extends parent class{
  }
  

super:
    

With super, you can access the parent class constructor

Calling super constructor must be written in the first line;
It is unreasonable to call the super constructor and this again, and an error will be reported
With super, you can access the properties of the parent class
With super, you can access the methods of the parent class

class Person{
	private String name;
	private int age;
	public String sex;
	
	
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
		
	}
	
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void say() {
		System.out.println("I am:"+name+",I this year:"+age);
	}
}

class Student extends Person{

	public Student() {
		super("Nobody",1);
		super.sex = "male";
		super.setName("Ha ha mirror");
	}

	
	
}



rewrite

package secondThird;
/**
 * Override rule
 * 1.The parameter list must be exactly the same as the overridden method;
 * 2.The return value type must be exactly the same as the return value of the overridden method;
 * 3.The access permission cannot be lower than that of the overridden method in the parent class
 * 4.A member method of a parent class can only be overridden by its class
 * 5.Methods declared as static and private cannot be overridden, but can be declared again
 * @author 23802
 *
 *Interview questions
 *java The difference between override and overload in
 *     1.Location of occurrence
 *        Overloading: in a class
 *        Overrides: in child parent classes
 *     2.Parameter list limit
 *        Overload: must be different
 *        Overrides must be the same
 *     3.return type
 *       Overload: independent of return value type
 *       Override: return value types must be consistent
 *     4.Access rights
 *        Overload: independent of access rights
 *        Override: the method permission of the child must not be lower than that of the parent
 *  
 *     5.exception handling
 *       Overload: independent of exception
 *       Rewrite: the exception range can be smaller, but new exceptions cannot be thrown
 */

public class Demo2 {
	
	public static void main(String[] args) {
		Student2 s = new Student2();
		s.say();
	}

}

class Person2{
	public void say() {
		System.out.println("Hoe standing grain gradually pawning a midday,Sweat drops under the grass");
	}
}

class Student2 extends Person2{
	public void say() {
		System.out.println("abed, I see a silver light,The glass is frosted");
	}
	
}



final keyword


  final is used to modify attributes and variables
         The variable has become a constant and cannot be assigned a value
         The local variable modified by final can only be assigned once (it can be declared before assignment)
         The member attribute of final modifier must be assigned at the time of declaration
         Public static final        
         Constant naming conventions:
               It consists of one or more words. Words must be separated by underscores. All letters of the word are capitalized
               For example: sql_intest
   final is used to decorate classes
      The class decorated by final cannot be inherited
   final is used to modify the method
       The method of final modification cannot be overridden

final int b=0;
	public static void main(String[] args) {
		final int a;
		a =20;
		

abstract class

Abstract classes must be declared with abstract class
An abstract class can have no abstract methods. Abstract methods must be written in an abstract class or interface.
Format:
Abstract class name {/ / abstract class

public abstract void method name (); / / abstract method, declared but not implemented
}

common problem

1. Can abstract classes use final declarations?
No, because the final modified class cannot have subclasses, and the abstract class must have subclasses to be meaningful, so it cannot.
2. Can an abstract class have a constructor?
There can be construction methods, and the process of subclass object instantiation is the same as that of ordinary class inheritance. It is necessary to call the construction method in the parent class (parameterless by default), and then call the subclass's own construction method.

The difference between abstract classes and ordinary classes

1. The abstract class must be decorated with public or protected (if it is private, the subclass cannot inherit and cannot implement its abstract methods).
The default is public
2. Abstract classes cannot use the new keyword to create objects, but when subclasses create objects, the abstract parent class will also be instantiated by the JVM.
3. If a subclass inherits an abstract class, it must implement all its abstract methods. If there are unimplemented abstract methods, the subclass must also be defined as an abstract class

Interface

If all methods in a class are abstract methods and all attributes are global constants, then this class can be defined as an interface.
Define format:
Interface interface name{
Global constant;
Abstract methods;
}

Abbreviations for global constants and abstract classes

Because the interface itself is composed of global constants and abstract methods, the member definitions in the interface can be abbreviated as:
1. When writing global constants, you can omit the public static final keyword, for example:
public static final String INFO = "content";
After abbreviation:
String INFO = "content";
2. When writing abstract methods, you can omit the public abstract keyword, for example:
public abstract void print() ;
After abbreviation:
void print() ;

Interfaces can be implemented in multiple ways:
Format:
class subclass implements parent interface 1, parent interface 2{
}
The above code is called the implementation of the interface. If a class wants to implement the interface and inherit the abstract class, it can be written in the following format:
Class subclass extends, parent class implements, parent interface 1, parent interface 2{
}

Because interfaces are abstract parts and there is no specific implementation, multiple inheritance is allowed, for example:
interface C extends A,B{
}

polymorphic

Conversion similar to basic data type:
·Upward Transformation: change a child class instance into a parent class instance
|-Format: parent class, parent class object = child class instance;
·Downward Transformation: change the parent class instance into a child class instance
|-Format: subclass subclass object = (subclass) parent class instance;

instanceof

effect:
To determine whether an object is an instance of a specified class, you can use the instanceof keyword
Format:
Instantiate the object instanceof class. / / this operation returns boolean data

public static void say(Person p) {
		//How to determine which form (which subclass object) the incoming object is of this type
		if (p instanceof Nurse) {
			Nurse n = (Nurse)p;
			n.say();
		}else {
			System.out.println("The student form must be passed in to execute");
		}
		
	}

Object

The Object class is the parent class (base class) of all classes. If a class does not explicitly inherit a specific class, it will inherit the Object class by default. Using Object can receive any reference data type

package Demo;

public class Demo1 {

	public static void main(String[] args) {
		String text = "123";
		say("123");
		int a =10;
		say(a);
	}
	
	public static void say(Object o) {
		System.out.println(o);
	}
}

toString

It is recommended to override the toString method in Object. This method is used to return the string representation of the Object.
The toString method of Object returns the memory address of the Object

public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
Person p = new Person("Zhang San",18);
		System.out.println(p);

equals

Five features when overriding the equals method:
Reflexivity: x.equals(x) should return true for any non empty reference value X.
Symmetry: for any non null reference values X and y, x.equals(y) should return true if and only if y.equals(x) returns true.
Transitivity: for any non null reference values x, y, and Z, if x.equals(y) returns true, then y.equals(z) returns true, and then
x.equals(z) should return true.
Consistency: for any non null reference values X and y, multiple calls to x.equals(y) always return true or always return false, provided the object is not modified
The information used in the equals comparison on.
Nonempty: x.equals(null) should return false for any nonempty reference value X.

Shortcut key shift+alt+s

Inner class

Member inner class

Member inner class is the most common inner class. Its definition is located inside another class, as shown in the following form:

class Outer {
private double x = 0;
public Outer(double x) {
this.x = x;
}
class Inner { //Inner class
public void say() {
System.out.println("x="+x);
}
}
}

External use member internal class

Outter outter = new Outter();
Outter.Inner inner = outter.new Inner();

Local inner class

A local internal class is a class defined in a method or scope. The difference between it and a member internal class is that the access of a local internal class is limited to the method or scope.

Anonymous Inner Class

new parent class constructor (parameter list) | implementation interface ()
{
//The body part of an anonymous inner class
}

Person p = new Person() {
			public void say() {
				System.out.println("Speechless");
			}
		};

be careful:

In the process of using anonymous inner classes, we need to pay attention to the following points:
1. When using anonymous inner classes, we must inherit a class or implement an interface, but we can't have both. At the same time, we can only inherit one class or interface
The designer implements an interface.
2. Constructors cannot be defined in anonymous inner classes.
3. No static member variables or static methods can exist in an anonymous inner class.
4. Anonymous inner classes are local inner classes, so all restrictions on local inner classes also apply to anonymous inner classes.
5. Anonymous inner classes cannot be abstract. They must implement all abstract methods of inherited classes or implemented interfaces.
6. Only local variables of type final can be accessed

Static inner class

Static internal classes are also classes defined in another class, but there is a keyword static in front of the class.
The static inner class does not need to depend on the external class object, which is similar to the static member property of the class, and it cannot use the non static members of the external class
Variable or method

.

public class Book {

	static class Info{
		public void say() {
			System.out.println("This is an information class");
		}
	}
}
Book.Info info = new Book.Info();
		info.say();

Packing and unpacking operations

Sn basic data type packing class
1 int Integer
2 char Character
3 float Float
4 double Double
5 boolean Boolean
6 byte Byte
7 short Short
8 long Long

        //Manual packing
		Integer i = new Integer(300);
		//s = manual unpacking
		int j = i.intValue();
		System.out.println(j);
		
		//Automatic packing 1.5 start
		Integer k = 23;
		//Automatic unpacking
		int a = k;
		System.out.println(a);

Strong rotation

Scanner input = new Scanner(System.in);
		System.out.println("Please enter the content:");
		String text = input.nextLine();
		
		int x = Integer.parseInt(text);
		
		System.out.println(x+1);

Variable parameters

When parameters are defined in a method, one-to-one corresponding parameters must be passed in when calling. However, new functions are provided after JDK 1.5, which can root
Automatically pass in any number of parameters as needed.
Syntax:
Return value type method name (data type... Parameter name){
//Parameters are received as an array inside the method
}
be careful:
Variable parameters can only appear at the end of the parameter list.

public static void main(String[] args) {
		System.out.println(sum(10,11,12,13));

	}

	/**
	 * int...nums:Represents a variable parameter. 0 to n numbers can be passed when calling
	 * Inside the method, variable parameters are represented by an array as a carrier
	 * @param nums
	 * @return
	 */
	public static int sum(int... nums) {
		int n = 0;
		for (int i = 0; i < nums.length; i++) {
			n+=nums[i];
		}
		return n;
	}

error

Public static int sum (int... Num, string a) (variable parameters can only appear at the end of the parameter list)

recursion

Recursion, in mathematics and computer science, refers to the use of the method itself in the definition of the method, that is, a recursive algorithm is an algorithm that directly or indirectly calls its own method.

Tags: Java

Posted on Sun, 19 Sep 2021 22:40:15 -0400 by flashroiem