Java object oriented (6)
instanceof and type conversion
instanceof
-
You can determine whether there is a relationship between two classes (such as parent-child relationship or itself)
-
Example 1:
Parent class:
public class Person { }
Subclass 1:
public class Student extends Person { }
Subclass 2:
public class Teacher extends Person { }
Main function:
public class Application { public static void main(String[] args) { //Person>Student Person s1 = new Student(); Student s2 = new Student(); System.out.println(s1 instanceof Person);//It matters System.out.println(s2 instanceof Person);//Paternity System.out.println(s2 instanceof Student);//itself System.out.println(s1 instanceof Student);//It matters System.out.println(s1 instanceof Object);//Paternity //Object>Person>Teacher //Object>Person>Student //There is no relationship between Teacher and Student System.out.println(s1 instanceof Teacher);//It doesn't matter. } } //Output: true true true true true false
Note:
System.out.println(s2 instanceof Teacher);//No, an error will be reported because Student and Teacher are of the same level and have no parent-child relationship
Type conversion
-
Conversion of basic types: conversion from high to low requires forced conversion; Low to high does not require forced conversion
-
Conversion of parent and child classes:
-
The conversion from high (parent) to low (child) requires forced conversion; Conversion from low to high is not required
-
The conversion must ensure that the two classes are related. For example, the parent class needs to point to the child class to cast the parent class into a child class
-
Converting a subclass to a parent class will lose its original method
-
Example 1 (CAST):
Parent class:
public class Person { public void see(){ System.out.println("see"); } }
Subclass:
public class Student extends Person { public void say(){ System.out.println("say"); } }
Main function:
public class Application { public static void main(String[] args) { Person obj1 = new Student(); obj1.see(); //Cast parent class to child class Student std = (Student) obj1; //After the cast, you can call the methods of the subclass //Cast 1 std.say(); std.see(); //Cast 2 ((Student) obj1).say(); } } //Output: see say see say
-
Example 2 (low to high, not forced):
Parent class:
public class Person { public void see(){ System.out.println("see"); } }
Subclass:
public class Student extends Person { public void say(){ System.out.println("say"); } }
Main function:
public class Application { public static void main(String[] args) { Student std = new Student(); //The subclass is converted to the parent class directly without coercion //Subclass to parent class will lose the original method of subclass Person obj2 = std; obj2.see(); } } //Output: see
-
-
Conditions for type conversion:
- The parent class reference points to the child class object;
- Convert the subclass into the parent class and transform upward;
- Convert a parent class to a child class, transform downward, and force conversion;
- Type conversion facilitates method calls and reduces duplicate code! concise
static keyword
-
Static methods and properties:
public class Student { private static int age;//Static properties private double score;//Non static attribute //Non static method public void run(){ } //Static method public static void go(){ } public static void main(String[] args) { //Static method usage go(); //Use of non static methods Student student = new Student(); student.run(); //Static attribute usage (output) System.out.println(age); //Non static attribute use (output) Student student2 = new Student(); System.out.println(student2.score); } }
-
Execution order of anonymous code block, static code block and construction method:
- Static code block
- Anonymous code block
- Construction method
public class Person { //Anonymous code block //2. You can also assign an initial value { System.out.println("Anonymous code block"); } //Static code block //1. Execute only once static { System.out.println("Static code block"); } //Construction method //3 public Person(){ System.out.println("Construction method"); } public static void main(String[] args) { Person person = new Person(); } } //Output: Static code block Anonymous code block Construction method
-
Static import package:
-
The method can be used directly
public class Application { public static void main(String[] args) { //Math. The random () method generates random numbers //Because you are importing static packages, you can use random() directly System.out.println(random()); } } //Output: 0.5566090462832636
-
-
final: define constants. After a class is defined as a constant, it cannot be inherited; Once a quantity is defined as a constant, it cannot be modified like a variable
abstract class
-
The classes modified by abstract are abstract classes
-
The definition abstraction has only the name of the method and no method implementation
-
Because the abstract method in the abstract class has no implementation process, if the subclass inheriting the abstract class with abstract method is not an abstract class, the subclass must override the abstract method (write the implementation process for the abstract method)
-
If a subclass that inherits an abstract class with abstract methods is an abstract class, you do not need to override the abstract method (because there can be abstract methods in the abstract class)
-
Extensions can only implement single inheritance, but interfaces can inherit more
-
be careful:
- Abstract classes can't be new. They can only be implemented by subclasses: constraints! (abstract classes cannot be instantiated)
- Only abstract classes can have abstract methods
- Abstract classes can have ordinary methods, but ordinary classes cannot have abstract methods
-
give an example
Abstract class:
public abstract class Action { //Constraint ~ someone helps us implement it (we don't need to write the implementation process in the abstract class) //Abstract abstract abstract method, only the name of the method, no method implementation public abstract void doSomething(); }
Subclass:
//Because the abstract method in the abstract class has no implementation process, the class that inherits it must override the abstract method (write the implementation process for the abstract method) public class A extends Action{ @Override //Override abstract methods public void doSomething() { } }
Interface
Differences between interface and other classes
- Common class: only concrete implementation
- Abstract classes: both concrete implementations and specifications (abstract methods) exist
- Interface: only specifications ----- you can't write methods ~ professional constraints! Constraint and implementation classification: interface oriented programming
Difference between interface and inheritance
- Class has the implementation of methods
- Interface
The essence of interface
- An interface is a specification that defines a set of rules. Realize "if it's you, you must..." Thought of
- The essence of an interface is a contract, just like the laws among us. After it is formulated, everyone will abide by it
Interface attention
-
The interface can only write methods, not the implementation of methods
-
The methods of the interface are all public abstract by default, so the public abstract can be written directly without writing the return value type and method name
-
Class implements the interface through implements
-
Class can implement multiple interfaces separated by commas
-
Class to implement the interface, you must override all the methods in the interface
-
Constants can be defined in the interface. The defined variables are public static final by default, that is, they are constants by default (but constants are generally not defined in the interface)
-
give an example:
Interface 1:
public interface TimeService { void timer(); }
Interface 2:
public interface UserService { //The methods of the interface are public abstract by default, so the public abstract can be written directly without writing the return value type and method name void add(String name);//increase void delete(String name);//delete void update(String name);//modify void query(String name);//query }
Class (implementation interface):
//Class can implement the interface: implements //The class that implements the interface must implement all the methods of the interface //Interface implements multiple inheritance public class UserServiceImpl implements UserService,TimeService{ @Override public void add(String name) { } @Override public void delete(String name) { } @Override public void update(String name) { } @Override public void query(String name) { } @Override public void timer() { } }
Function of interface
- constraint
- Define some methods for different people to implement ~ for example, 10 classes jointly implement one interface
- public abstract: the default method (abstract method) in the interface
- public static final: definition of static constants (the default quantities in the interface are constants)
- The interface cannot be instantiated ~ because there is no constructor in the interface~
- Implementations (interfaces) can implement multiple (interfaces are separated by commas)
- To implement an interface through implements, you must override the methods in the interface
Inner class
Inner class
-
What is an inner class:
Writing another class in a class is an inner class
-
Functions of internal classes:
- The inner class can directly use the private variables of the outer class
-
give an example:
Class:
public class Outer { private int id=10; public void out(){ System.out.println("This is the method of an external class"); } //Inner class class Inner{ public void in(){ System.out.println("This is the method of the inner class"); } public void getID(){ System.out.println(id); } } }
Main function:
public class Application { public static void main(String[] args) { //Instantiation class //Instantiate the external class first Outer outer = new Outer(); //Calling methods of external classes //An external class cannot call a method of an internal class outer.out(); //Only when you instantiate an external class first can you instantiate an internal class through an external class //Instantiate the inner class through the outer class Outer.Inner inner = outer.new Inner(); //Call the method of the inner class //An inner class cannot call a method of an outer class inner.in(); inner.getID(); } } //Output: This is the method of an external class This is the method of the inner class 10
Static inner class
-
public static class name: static internal class
-
Methods in static internal classes cannot directly call private non static properties of external classes. Because static generation comes first, non static generation comes later, and the later generation cannot be called first by the first generation
-
give an example:
class
public class Outer { private static int id=10; public void out(){ System.out.println("This is the method of an external class"); } //Static inner class public static class Inner{ public void in(){ System.out.println("This is the method of a static inner class"); } public void getID(){ System.out.println(id); } } }
About class
-
There can only be one public class in a java class, but there can be multiple class classes
-
give an example:
public class Outer2 { } //There can only be one public class in a java class, but there can be multiple class classes class A{ }
Local inner class
-
Classes written in methods
-
give an example:
public class Outer3 { //Local inner class public void method(){ class Inner{ public void in(){ System.out.println("This is a local inner class"); } } } }
Anonymous Inner Class
-
give an example:
public class Application { public static void main(String[] args) { //Use of anonymous inner classes new B1().eat(); new B2(){ @Override public void see() { } }; } } class B1{ public void eat(){ System.out.println("Internal class 1"); } } interface B2{ void see(); }