Day 14
Formal parameter problem
If the formal parameter of a method is a reference type, the actual parameter is passed when calling the method
If it is a concrete class, the method is called directly, and the actual parameters need to pass the object of the current concrete class
If it is an abstract class, the subclass object of the abstract class needs to be passed when calling the method (abstract class polymorphism)
If it is an interface, when calling a method, the actual parameters need to pass the sub implementation class object of the current excuse (excuse polymorphism)
//Define an interface interface Love{ void love() ; } //LoveDemo class class LoveDemo { //Love love = new LovePerson() p public void function(Love l){//The formal parameter of the method is the interface type, and the sub implementation class object of the interface must be passed l.love();//love.love() ; } } //Define a sub implementation class of an interface class LovePerson implements Love{ @Override public void love() { System.out.println("Love life,love Java,Love Gao Yuanyuan..."); } } //Test class public class LoveTest { public static void main(String[] args) { //You need to access the function method in the LoveDemo class //Create a LoveDemo class object LoveDemo ld = new LoveDemo() ; //Love love = new Love() ; // Interface cannot be instantiated //Interface polymorphism Love love = new LovePerson() ; ld.function(love) ; System.out.println("------------------------"); //Anonymous object new LoveDemo().function(new LovePerson()); } }
If the return value of a method is a reference type, how will it be returned at the end of the method?
The method return of a concrete class is to put back the object of the current concrete class
An abstract class needs to return subclass objects of the abstract class
The interface needs to return the sub implementation class object of the interface
//Define an interface //Marriage interface interface Mary{ public abstract void mary() ; } //MaryDemo class class MaryDemo{ public Mary function(){//The return value type is the interface type, //return ? // Mary mary = new Mary() ; // Interface cannot be instantiated //Subclass objects that need to provide interfaces //Interface polymorphism // Mary mary = new You() ; // return mary ; //One step return new You() ; } } //Define a class class You implements Mary{ @Override public void mary() { System.out.println("Get married,Very happy..."); } } //Test class public class LoveTest { public static void main(String[] args) { //Call the function method in the MaryDemo class? MaryDemo maryDemo = new MaryDemo() ; Mary mary = maryDemo.function(); mary.mary(); System.out.println("----------------------"); //Anonymous object Mary m = new MaryDemo().function(); m.mary(); } }
package keyword
Package in java is equivalent to a folder in windows. The function of package is to solve the problem of conflict caused by repeated class names and facilitate the release of software
Package format: package name;
The naming convention for package names is that package names are all lowercase
The package statement must be the first statement in java, and the import statement should be placed under the package statement
If a class is added with a rainstorm Bureau, the complete class name of the class is the package name and class name
A java file can only have one package statement
When compiling, use java -d to specify the storage path of class files and java source files
Permission modifier
Permission modifiers include default, private, protected, and public
After being modified by private, it can only be accessed in the current class of the same package. The default modifier default can be accessed in the current class and subclasses of the current class. After being modified by protected, it can be accessed in the current class, classes under the same package and classes under different packages. public can be accessed in all the first three places, and can also be accessed in unrelated classes of different packages
Inner class
Is to define another class in one class,
If class B is defined in class A, then B is the inner class of a and a is the outer class of B
Internal classes can access members of external classes, including private ones
//External class class Outer{ //Member variable public int num = 100 ; private int num2 = 200 ; class Inner{ //Member inner class //A member method of a class within a member public void method(){ System.out.println("method Inner"); System.out.println(); System.out.println(num2); } } //Member methods of external classes public void show(){ //Method of the internal class of the accessed member --- > accessed by creating an internal class object //method() ; Wrong -- Method method method of this class accessed Inner inner = new Inner() ; inner.method(); } }
Methods for external classes to access internal class members:
//External class name Internal class name object name = external class object Inner class object class Outer2{ private int num = 20 ; //Member inner class class Inner2{ public void show(){ System.out.println(num); } } //Member methods of external classes public void method(){ // Create an inner class object to access the member methods of the inner class } } //Test class public class InnerClassDemo2 { public static void main(String[] args) { //External class name Internal class name object name = external class object Internal class objects; //Applicable to: directly accessing members of the internal class of a member through an external class (prerequisite: the internal class of the current member is a non static class) Outer2.Inner2 oi = new Outer2().new Inner2() ; oi.show() ; } }
In general, private is added to the internal class. For data security, if you want to access the internal class, you need to access it indirectly through the public methods of the external class
//The external class directly accesses the members of the internal class //Format: external class name Internal class name object name = external class object Internal class objects;
If the internal of the current member is static, the internal methods, whether static or non static, can only access the static members of the external class, including private
Methods that want to directly locate the internal class of static members can be accessed by directly accessing the class, that is:
External class name Internal class name object name = new external class name Internal class name ();
class Outer3{ //Define non static member variables public int num = 50 ; private static int num2 = 20 ; //Define member inner class: static ----- > static member inner class can be regarded as static member of external class static class Inner3{//At this time, all classes are static public void show(){ // System.out.println(num); System.out.println(num2); } public static void show2(){ // System.out.println(num); System.out.println(num2); } } } //Test class public class InnerClassDemo3 { public static void main(String[] args) { // External class name Internal class name object name = external class object Internal class objects; //Outer3.Inner3 oi = new Outer3().new Inner3() ; It doesn't work anymore // External class name Internal class name object name = new external class name Internal class name (); Outer3.Inner3 oi = new Outer3.Inner3() ; oi.show(); oi.show2() ; //Static -- object name access is not recommended System.out.println("------------------------------"); //Another way of show2() Outer3.Inner3.show2(); //show2() static method } }