Learn Java EE day 15 notes

Posted by psych0 on Sat, 22 Feb 2020 12:41:28 +0100

Learn Java EE day 15

  • Scene two:
    We use the parent class as the method return value to implement polymorphism, so that the method can return different subclass objects.
public class TestPolymorphic {
	public static void main(String[] args) {
		Employee emp = new Employee();
		emp.name = "tom";

		
		Vehicle myVeh = emp.buyVeh(1200000D);
		
		if(myVeh != null) {
			if(myVeh instanceof Bus) {
				Bus bus = (Bus)myVeh;
				emp.goHome(bus);
			}else if(myVeh instanceof Car){
				Car car = (Car)myVeh;
				emp.goHome(car);
			}else if(myVeh instanceof Bicycle) {
				Bicycle bic = (Bicycle)myVeh;
				emp.goHome(bic);
			}
		}
	}
}

//staff
class Employee{
	
	String name;
	
	//Parent type as method parameter, realizing polymorphism
	public void goHome(Vehicle veh){
		System.out.print("Are you off duty?"+name+"ride");
		veh.run();
	}
	
	public Vehicle buyVeh(double money) {//The return value is the reference parent type, realizing polymorphism
		if(money > 1000000.0) {
			Bus bus = new Bus();
			bus.type = "Bus";
			bus.speed = 50;
			bus.price = 1000000D;
			bus.seatNum = 20;
			return bus;
		}else if(money > 500000.0) {
			Car car = new Car();
			car.type = "A car";
			car.brand = "BWM";
			car.price = 500000.0;
			car.speed = 60;
			return car;
		}else if(money > 2000.0) {
			Bicycle bic = new Bicycle();
			bic.type = "Bicycle";
			bic.price = 2000D;
			bic.speed = 20;
			bic.color = "white";
			return bic;
		}else {
			return null;
		}
	}
}

class Vehicle{
	String type;
	int speed;
	double price;
	
	public void run() {
		System.out.println("A value"+price+"Of"+type+"with"+speed+"/h On the road");
	}
}
class Car extends Vehicle{
	String brand;
	public void run() {
		System.out.println("A value"+price+"Of"+brand+"Brand"+type+"with"+speed+"/h On the road");
	}
}
class Bus extends Vehicle{
	int seatNum;
	public void run() {
		System.out.println("A value"+price+"Yes"+seatNum+"Seating"+type+"with"+speed+"/h On the road");
	}
}
class Bicycle extends Vehicle{
	String color;
	public void run() {
		System.out.println("A value"+price+"Of"+color+"Chromatic"+type+"with"+speed+"/h On the road");
	}
}

8.6.4 upward transformation (packing)

public class TestConvert {
	public static void main(String[] args) {
		Animal a = new Dog();
	}
}
class Animal{
	public void eat() {
		System.out.println("Animal eating...");
	}
}
class Dog extends Animal{
	public void eat() {
		System.out.println("Dogs are eating bones...");
	}
}
  • In the parent class reference, the real subclass object is preserved, which is called the upward transformation (that is, the core concept of polymorphism);
  • Note: only properties and methods declared in Animal can be called;

8.6.5 downward transformation (unpacking)

public class TestConvert {
	public static void main(String[] args) {
		Animal a = new Dog();
		Dog dog = (Dog)a;
	}
}
class Animal{
	public void eat() {
		System.out.println("Animal eating...");
	}
}
class Dog extends Animal{
	public void eat() {
		System.out.println("Dogs are eating bones...");
	}
}
  • It is called downward transformation that the real subclass object in the parent class reference is strongly transferred back to the subclass's own type;
  • Note: only when the real type of the subclass is converted back, the unique properties and methods of the subclass can be called;

8.6.6 type conversion exception

8.6.7 instanceof keyword

  • Before the downward transformation, we should judge the real type of the object in the quotation to ensure the correctness of the type transformation;
  • Syntax: reference instanceof type / / return boolean type result;
public class TestConvert2 {
	public static void main(String[] args) {
		Animal a = new Dog();
		
		if(a instanceof Dog) {
			Dog dog = (Dog)a;
			dog.eat();
		}else if(a instanceof Cat) {
			Cat cat = (Cat)a;
			cat.eat();
		}
	}
}
  • When the object type stored in the "a" reference is Dog, type conversion is carried out, and then unique methods in Dog are called;

8.7 summary

  • There are two application scenarios of polymorphism:
    The parent class is used as the parameter of the method to realize polymorphism;
    When calling a method, the types of arguments that can be passed include: object of this type + all its subclass objects;
    Use the parent class as the return value of the method to realize polymorphism:
    After the method is called, the result types include: object of this type + all its subclass objects;

  • The role of polymorphism:
    The differences between the shielding subclasses;
    Flexible and low coupling;

9. Three modifiers

9.1 abstract

9.1.1 what is abstraction

  • Specious, like but not; with some characteristics of the object, but not complete; abstraction in life:
  • For example, when we search for pictures of animals on Baidu, the search results are all sub objects of "animals" instead of "animals";

Objects that should not be created:

public class TestAbstract {
	public static void main(String[] args) {
		Animal2 a = new Animal2();
	}
}
class Animal2{
	String breed;
int age;
	String sex;
	public Animal2() {}
	public void eat() {}
	public void sleep() {}
}
  • Animal is only a kind of object that can eat and sleep. There is no other behavior, which is not specific and complete;
  • The program is used to simulate the real world and solve the real problems. In the real world, there are all the specific subclass objects of "animals", and there is no "animals" object.
  • Therefore, an Animal should not be created as an object independently;
  • So, how to limit the creation of such objects?

9.1.2 abstract class

  • Apply: abstract modifier class, this class cannot be a new object.
public class TestAbstract {
	public static void main(String[] args) {
		//Animal2 a = new Animal2(); animal2 is abstract and cannot be instantiated
	}
}
abstract class Animal2{
	String breed;
	int age;
	String sex;
	public Animal2() {}
	public void eat() {}
	public void sleep() {}
}
  • The class modified by abstract is called abstract class;
  • Abstract class means incomplete class, not concrete class. Abstract class object cannot exist independently, that is, it cannot be new object;

9.1.3 the role of abstract classes

  • Effect:
    It can be inherited by subclasses, providing common attributes and methods;
    Can be declared as a reference, forcing polymorphism;
public class TestAbstract {
	public static void main(String[] args) {
		//Animal2 a = new Animal2(); animal2 is abstract and cannot be instantiated
		Animal2 a1 = new Dog2();
		Animal2 a2 = new Cat2();
	}
}
abstract class Animal2{
	String breed;
	int age;
	String sex;
	public Animal2() {}
	public void eat() {}
	public void sleep() {}
}
class Dog2 extends Animal2{}
class Cat2 extends Animal2{}
  • Abstract parent class, as a part of the subclass, depends on the existence of the subclass object. It is composed of the common parent class and the unique subclass;

9.1.4 abstract method

Methods that should not be implemented:

abstract class Animal2{
	String breed;
	int age;
	String sex;
	public Animal2() {}
	public void eat() {
		System.out.println("Animals are eating...");
	}
	public void sleep() {
		System.out.println("Animals are sleeping...");
	}
}
class Dog2 extends Animal2{}//Requirement: eat() in Dog should output "Dog is eating Dog food"
class Cat2 extends Animal2{}//Requirement: eat() in Cat should output "Cat is eating Cat food"
  • The method provided by the parent class is difficult to meet the different needs of the child class. If it is not defined, it means that all animals will not eat or sleep. If it is defined, it is slightly redundant, and most of it will be covered by the child class.
  • Method declaration is necessary and method implementation is redundant;
abstract class Animal2{
	String breed;
	int age;
	String sex;
	public Animal2() {}
	public abstract void eat();	//abstract method
	public void sleep() {}
}
class Dog2 extends Animal2{
	public void eat() {
		System.out.println("Dogs are eating cat food");
	}
}
class Cat2 extends Animal2{
	public void eat() {
		System.out.println("The cat is eating cat food");
	}
}
  • The method modified by abstract is called abstract method. Only method declaration, no method implementation ({} part). Incomplete methods must be contained in abstract classes.
  • After the generation of inheritance relationship, the subclass must cover all the abstract methods of the parent class, otherwise the subclass is still an abstract class;

9.1.5 summary

  • abstract modifier class: cannot new object, but can declare reference;
  • Abstract decorated method: only declared method, no method implementation. (to be included in an abstract class)
  • There are not necessarily abstract methods in abstract classes, but some abstract methods must be abstract classes;
  • After the subclass inherits the abstract class, it must cover all the abstract methods of the parent class, otherwise the subclass is still the abstract class;
Published 18 original articles, won praise 0, visited 259
Private letter follow

Topics: Java