abstract class
- The method modified with abstract has no method body, only declaration. The definition is a specification, which is to provide concrete implementation for abstract methods
1. Classes with abstract methods can only be defined as abstract classes
2. Abstract classes cannot be instantiated, that is, they cannot be instantiated with new
3. Abstract classes can contain attributes, methods and construction methods, but construction methods cannot be instantiated with new, but can only be called by subclasses
4. Abstract classes can only be inherited
5. Abstract methods must be implemented by subclasses
package cpm.bjsxt.test2; abstract class Animal{ abstract public void shout();//Abstract method } public class chouxiang { public static void main(String[] args) { Dog a = new Dog(); a.shout(); a.seeDoor(); } } class Dog extends Animal{ //The subclass must implement the abstract method of the parent class, otherwise the compilation error public void shout(){ System.out.println("Woof, woof!"); } public void seeDoor(){ System.out.println("Watch the door..."); } } class person{ public void study();//Report an error, or add abstract or {...} }
Interface
The interface is called the method, defined by an interface, "implements this interface method" in the class. Multiple inheritance can be realized through the interface.
The following implements an interface:
package cpm.bjsxt.test2; import jdk.*; import cpm.bjsxt.test2.SayHello.*; public class jiekou { public static void main(String[] args) { China china=new China(); china.say(); china.add(1,8); } }
package cpm.bjsxt.test2; /** To implement the interface, a class only needs to use the implements keyword. The implementation class must implement all the methods in the interface public class Implementation class name implements interface{ //Method of implementing interface }*/ public class China implements SayHello,Hello { @Override public void say() { System.out.println("Hello"); } @Override public void add(int a, int b) { System.out.println(a+b); } }
package cpm.bjsxt.test2; /**Definition of interface public interface Interface name{ //Constants can be defined //Methods have only method declarations and are public. public void Method name (); ... }*/ public interface SayHello { //Interfaces can only define constants, which are always of type public static final public static final double a=1.777; //All methods in the interface have no method body public void say(); }
package cpm.bjsxt.test2; public interface Hello { public void add(int a,int b); }
encapsulation
Three steps of packaging
-
1. Attribute privatization
-
2. Write the public value assignment method getter/setter method
-
3. Carry out reasonable verification in the method of assignment
package com.fengzhuang; public class fz1 { //Property privatization private String name; private int id; //Write get/set methods public void setName(String name) { this.name = name; }//assignment public String getName(){ return name; }//Value public static void main(String[] args) { fz1 a=new fz1(); a.setName("cll"); a.getName(); } }
this keyword supplement
package com.fengzhuang; public class this1 { private int a;//Member variable public void setA(int a) { this.a = a;//local variable } public int getA() { return a; } public this1(){ System.out.println("aaaaa"); }//Default construction method, no parameters public this1(int a){ this();//Call parameterless constructor System.out.println(a+5); }//With parameters public static void main(String[] args) { this1 a=new this1(5);//Here, the constructor will be called automatically, and you can choose whether there are parameters or no parameters System.out.println(a.getA()); } }
super
super cannot appear in a static method.
Call the non private property super Attribute name
Call non private method super()
package cpm.bjsxt.test; public class zObject extends Anmials{ public zObject(){//Subclass super();//Can only appear on the first line of the constructor super.setId(5);//Call the properties of the parent class System.out.println("zObject Output of parameterless construction method of class"+super.getId()); } public static void main(String[] args) { zObject a=new zObject(); } } class Anmials{//Parent class int id; public void setId(int id) { this.id = id; } public int getId() { return id; } public Anmials() { System.out.println("Anmials Output of parameterless construction method of class"); } }
Method rewriting, complementing, inheritance and composition
- inherit
- Syntax format inherited by Java:
public class child class name extends parent class name {...}
- Java will automatically add a parent class to every class that does not have a parent class, which is Object.
- Method rewriting
When both the subclass and the parent class have certain methods, and the methods of the subclass are more detailed, or the implementation functions are different, the method needs to be rewritten.
Rewriting condition ① must have inheritance relationship; Only the relationship between inheritance can have method rewriting
② The return value type, method name, number of parameters, parameter type and parameter order of the method must be completely consistent;
package cpm.bjsxt.test; public class Person /*extends Object*/{ int id; String name; public void rest(){ System.out.println("break"); } public static void main(String[] args) { Student s=new Student(); s.study(); s.rest(); System.out.println(s instanceof Person); } } class Student extends Person { int score; public void study(){ System.out.println("study"+this.name); } public void rest(){ System.out.println("Go back to bed"); } } class Employee extends Person{ public void rest(){ System.out.println("Rest in office area"); } }
File class_ Recursion_ Print directory tree structure
package com.bjsxt; import java.io.File; public class St { public static void main(String[] args) { File f=new File("D:\\movies\\mainland"); printFile(f,0); } /** * Print file information */ static void printFile(File file,int level){ //Output hierarchy for(int i=0;i<level;i++){ System.out.println("-"); } //Output file name System.out.println(file.getName()); //If file is a directory, get the list of sub files and do the same for each sub file if(file.isDirectory()){ File[] files=file.listFiles(); for(File temp:files){ //Call this method recursively printFile(temp,level+1); } } } }