The polymorphism of Day1 object-oriented
1.0 polymorphic format
2.0 use characteristics of member variables in polymorphism
public class FU { int num=10; public void meth() { System.out.println(num); } }
public class ZI extends FU{ int num=20; @Override public void meth() { System.out.println(num); } }
public class Main { public static void main(String[] args) { FU fu=new ZI(); System.out.println(fu.num); //10; fu.meth();//20 } }
3.0 characteristics of member method in polymorphism
public class FU { public void meth1() { System.out.println("AAA"); } public void methfu() { System.out.println("Parent specific method!"); } }
public class ZI extends FU{ public void methzi() { System.out.println("Subclass specific methods!" ); } }
public class Main { public static void main(String[] args) { FU fu=new ZI(); //fu.methzi() the parent class on the left has no methsi () fu.methfu();// If there is no one on the right, look up fu.meth1(); } }
4.0 upward and downward transformation of objects
public abstract class Animals { public abstract void eat(); }
public class Cat extends Animals { @Override public void eat() { System.out.println("Cats eat fish"); } public void catchmouse() { System.out.println("Cat and mouse"); } }
public class Main { public static void main(String[] args) { Animals animal=new Cat(); animal.eat(); //animal.catchmouse(); / / the parent class on the left has no such method //Downward transformation Cat cat=(Cat)animal; cat.catchmouse(); } }
Print results:
Cats eat fish
Cat and mouse
5.0 notebook USB interface case implementation
public interface USB { public abstract void opendevice(); public abstract void offdevice(); }
public class Computer { public void open() { System.out.println("Computer on"); } public void off() { System.out.println("Computer shutdown"); } public void opendevices(USB usb) { usb.opendevice(); if(usb instanceof Keyboard) { Keyboard keyboard=(Keyboard)usb; keyboard.type(); } else if(usb instanceof Mouse) { Mouse mouse=(Mouse) usb; ((Mouse) usb).click(); } usb.offdevice(); } }
public class Keyboard implements USB{ @Override public void opendevice() { System.out.println("Open keyboard"); } @Override public void offdevice() { System.out.println("turn off keyboard"); } public void type() { System.out.println("keyboard entry"); } }
public class Mouse implements USB{ @Override public void opendevice() { System.out.println("Turn on the mouse"); } @Override public void offdevice() { System.out.println("Mouse off"); } public void click() { System.out.println("Mouse click"); } }
public class Main { public static void main(String[] args) { Computer com=new Computer(); com.open(); //Prepare a mouse for your computer //Mouse mouse=new Mouse(); //USB usb=mouse; / / upward transformation USB usb=new Mouse();//Polymorphic approach com.opendevices(usb); Keyboard keyboard=new Keyboard();//No use of upward transformation, which can be understood as automatic transformation from small scope to large scope com.opendevices(keyboard); //com.opendevices(new Keyboard()) com.off(); } }
Print results:
Computer on
Turn on the mouse
Mouse click
Mouse off
Open keyboard
keyboard entry
turn off keyboard
Computer shutdown
Day2 final keyword
1.0 final keyword concept and four usages
2.0 final modifies a class
3.0 final modifies a local variable
public class People { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public People(String name) { this.name = name; } public People() {} }
public class FinalMain { public static void main(String[] args) { int num1=10; System.out.println(num1); num1=20; System.out.println(num1); final int num2=30; //num2=40 //num2=30 System.out.println(num2); final int num3; num3=40 ;//The right way! System.out.println(num3); People people1=new People("Jin Taiheng"); System.out.println(people1);//address people1=new People("Tian Shiguo"); System.out.println(people1); people1.setName("Park Zhimin");//The content can be changed, but the address cannot be changed for the reference type } }
Print results:
10
20
30
40
People@50cbc42f
People@75412c2f
4.0 final decorated member variable
public class Finaldemo { private final String name; //Assignment by construction method public Finaldemo() { name="Xu Suizhen"; } public Finaldemo(String name) { this.name = name; } public String getName() { return name; } }Day3 inner class
1.0 concept, classification and definition of internal classes
2.0 use of inner classes
public class Outer {//Indirect way class Inner{ public void methodinner() { System.out.println("Internal method execution!"); } } public void methodouter() { System.out.println("External method execution!"); new Inner().methodinner(); } }
public class Main { public static void main(String[] args) { Outer outer=new Outer(); outer.methodouter(); } }
Direct way
public class Outer { class Inner{ public void methodinner() { System.out.println("Internal method execution!"); } } public void methodouter() { System.out.println("External method execution!"); } }
public class Main { public static void main(String[] args) { Outer.Inner inner=new Outer().new Inner(); inner.methodinner(); } }
Print results:
Internal method execution!
3.0 access to variables with the same name of inner class
public class Outer { int num=10; class Inner { int num=20; public void method() { int num=30; System.out.println(num);//30 System.out.println(this.num);//20 System.out.println(Outer.this.num);//10 } } }
4.0 definition of local inner class
public class Outer { public void methodOuter() { class Inner { int num=10; public void metehod() { System.out.println(num); } } Inner inner=new Inner(); inner.metehod(); } }
public class Main { public static void main(String[] args) { Outer outer=new Outer(); outer.methodOuter(); } }
Print results:
10
5.0 the final problem of local inner class
6.0 anonymous inner class
1) Use
public interface Interface { public abstract void method(); }
public class Main { public static void main(String[] args) { Interface impl=new Interface() { @Override public void method() { System.out.println("Interface override override method execution"); } }; impl.method(); } }
Print results:
Interface override override method execution
2) Precautions
7.0 class as member variable
public class Weapon { private String device; public Weapon() { } public Weapon(String device) { this.device = device; } public String getDevice() { return device; } public void setDevice(String device) { this.device = device; } }
public class Hero { private String name; private int age; private Weapon weapon; public Hero() { } public Hero(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String Name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Weapon getWeapon() { return weapon; } public void setWeapon(Weapon weapon) { this.weapon = weapon; } public void crack() { System.out.println(getAge()+" "+getName()+" I did"+weapon.getDevice()); } }
public class Main { public static void main(String[] args) { Hero hero=new Hero("Daji",18); Weapon weapon=new Weapon(); weapon.setDevice("Enlarge move"); hero.setWeapon(weapon); hero.crack(); } }
Print results:
18 Daji makes a big move
8.0 use interface as member variable
public interface Skill { public abstract void device(); }
public class Interfaced implements Skill{ @Override public void device() { System.out.println("fight to win or die"); } }
public class Hero { private int age; private String name; private Skill skill; public Hero() { } public Hero(int age, String name, Skill skill) { this.age = age; this.name = name; this.skill = skill; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Skill getSkill() { return skill; } public void setSkill(Skill skill) { this.skill = skill; } public void crack() { System.out.print("I am"+getAge()+"Year old"+getName()+"Releasing skills"); skill.device(); } }
public class Main { public static void main(String[] args) { Interfaced impl=new Interfaced(); Hero hero=new Hero(); hero.setName("Han Xin"); hero.setAge(18); /* *The first implementation class to create Skill interface * hero.setSkill(impl); * hero.crack(); * I'm 18-year-old Han Xin. I'm putting out my skills * */ /* * The second is to use anonymous inner classes * Skill skill=new Skill() { @Override public void device() { System.out.println("*** } }; * hero.setSkill(skill); * hero.crack();//I'm 18-year-old Han Xin. I'm putting out my skills * */ /*The third kind uses anonymous objects and anonymous inner classes*/ hero.setSkill(new Skill() { @Override public void device() { System.out.println("fight to win or die"); } }); hero.crack();//I'm 18-year-old Han Xin. I'm putting out my skills } }