The formal parameters of the method and the return value type of the method

Formal parameters of methods and return value types of methods: anonymous objects, formal parameters of methods, return value types of methods


1. The formal parameter type of the method

        (1) The formal parameter is a basic type (too simple, omitted)

        (2) The formal parameter is a reference type --> class: what is needed is an object of this class

/*
	Formal parameters:
		Basic types (too simple to explain today)
		reference type
			Class name: (In fact, we have already talked about anonymous objects) What is needed is the object of this class
			Abstract class:
			interface
*/
class Student {
	public void study() {
		System.out.println("Good Good Study,Day Day Up");
	}
}

class StudentDemo {
	public void method(Student s) { //ss; ss = new Student();  Student s = new Student();
		s.study();
	}
}

class StudentTest {
	public static void main(String[] args) {
		//Requirement: I want to test the study() method of the Student class
		Student s = new Student();
		s.study();
		System.out.println("----------------");
		
		//Requirement 2: I want to test the method() method in the StudentDemo class
		StudentDemo sd = new StudentDemo();
		Student ss = new Student();
		sd.method(ss);
		System.out.println("----------------");
		
		// anonymous object usage
		new StudentDemo().method(new Student());
	}
}
        (3) The formal parameter is a reference type --> abstract class: what is needed is an object of a subclass of the abstract class (this is polymorphism)
/*
	Formal parameters:
		Basic types (too simple to explain today)
		reference type
			Class name: (in fact, we have already talked about anonymous objects) what is needed is the object of this class
			Abstract class: what is required is the abstract class subclass object
			interface
*/
abstract class Person {
	public abstract void study();
}

class PersonDemo {
	public void method(Person p) {//p; p = new Student();  Person p = new Student();   //多态
		p.study();
	}
}

//Define a concrete student class to implement the abstract class
class Student extends Person {
	public void study() {
		System.out.println("Good Good Study,Day Day Up");
	}
}

class PersonTest {
	public static void main(String[] args) {
		// Currently there is no way to use
		//Because the abstract class has no corresponding concrete class
		//Then, we should first define a concrete class
		//Requirement: I want to use the method() method in the PersonDemo class
		PersonDemo pd = new PersonDemo();
		Person p = new Student();
		pd.method(p);
	}
}

        (4) The formal parameter is a reference type -> interface: what is needed is an object of the implementation class of the interface (this is also polymorphism)


/*
	Formal parameters:
		Basic types (too simple to explain today)
		reference type
			Class name: (in fact, we have already talked about anonymous objects) what is needed is the object of this class
			Abstract class: what is required is the abstract class subclass object
			Interface: what is required is the implementation class object of the interface
*/
//Define a hobby interface
interface Love {
	public abstract void love();
}

class LoveDemo {
	public void method(Love l) { //l; l = new Teacher();  Love l = new Teacher(); 多态
		l.love();
	}
}

//Define the concrete class to implement the interface
class Teacher implements Love {
	public void love() {
		System.out.println("The teacher loves students, loves Java, loves Lin Qingxia");
	}
}

class TeacherTest {
	public static void main(String[] args) {
		//Requirement: I want to test the love() method in the LoveDemo class
		LoveDemo ld = new LoveDemo ();
		Love l = new Teacher();
		ld.method(l);
	}
}

2. Return value type

        (1) The return value is a basic type (too simple, omitted)

        (2) The return value is a reference type -> class: an object of this class is returned

/*
	return value type
		Basic types: (basic types are too simple, I am not going to explain)
		Reference type:
			Class: Returns an object of this class
			Abstract class:
			interface:
*/
class Student {
	public void study() {
		System.out.println("Good Good Study,Day Day Up");
	}
}

class StudentDemo {
	public Student getStudent() {
		//Student s = new Student();
		//Student ss = s;
		
		//Student s = new Student();
		//return s;
		return new Student();
	}
}

class StudentTest2 {
	public static void main(String[] args) {
		//Requirement: I want to use the study() method in the Student class
		//However, this time my requirement is, do not create the object of Student directly
		//Let you use StudentDemo to help you create objects
		StudentDemo sd = new StudentDemo();
		Student s = sd.getStudent(); //new Student(); Student s = new Student();
		s.study();
	}
}

        (3) The return value is a reference type -> abstract class: an object of a subclass of the abstract class is returned (this is polymorphism)

/*
	return value type
		Basic types: (basic types are too simple, I am not going to explain)
		Reference type:
			Class: Returns an object of this class
			Abstract class: Returns a subclass object of the abstract class
			interface:
*/
abstract class Person {
	public abstract void study();
}

class PersonDemo {
	public Person getPerson() {
		//Person p = new Student();
		//return p;
		
		return new Student();
	}
}

class Student extends Person {
	public void study() {
		System.out.println("Good Good Study,Day Day Up");
	}
}

class PersonTest2 {
	public static void main(String[] args) {
		//Requirement: I want to test the study() method in the Person class
		PersonDemo pd = new PersonDemo();
		Person p = pd.getPerson(); //new Student();  Person p = new Student(); 多态
		p.study();
	}
}
        (4) The return value is a reference type -> interface: it returns an object of the implementation class of the interface (this is also polymorphic)
/*
	return value type
		Basic types: (basic types are too simple, I am not going to explain)
		Reference type:
			Class: Returns an object of this class
			Abstract class: Returns a subclass object of the abstract class
			Interface: Returns the object of the class that implements the interface
*/
//Define a hobby interface
interface Love {
	public abstract void love();
}

class LoveDemo {
	public Love getLove() {
		//Love l = new Teacher();
		//return l;
		
		return new Teacher();
	}
}

//Define the concrete class to implement the interface
class Teacher implements Love {
	public void love() {
		System.out.println("The teacher loves students, loves Java, loves Lin Qingxia");
	}
}

class TeacherTest2 {
	public static void main(String[] args) {
		//How to test it?
		LoveDemo ld = new LoveDemo ();
		Love l = ld.getLove(); //new Teacher(); Love l = new Teacher(); 多态
		l.love();
	}
}

3. Chain programming: After each call is completed, an object is returned.

/*
	chain programming.
		Each time the method is called, an object is returned.
*/
class Student {
	public void study() {
		System.out.println("Good Good Study,Day Day Up");
	}
}

class StudentDemo {
	public Student getStudent() {
		return new Student();
	}
}

class StudentTest3 {
	public static void main(String[] args) {
		//How to call it?
		StudentDemo sd = new StudentDemo();
		//Student s = sd.getStudent();
		//s.study();
		
		// everyone pay attention
		sd.getStudent().study();
	}
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324377512&siteId=291194637