学习笔记——面向对象(继承)
一、学习重点

二、学习内容
public class Ch01 { public static void main(String[] args) { Biology biology=new Biology(); biology.age=20; biology.breath(); Animal animal=new Animal(); animal.age=3; animal.breath(); Person person=new Person(); person.age=30; person.name="ll"; person.eat(); person.breath(); } } package com.jsoft.morning; public class Biology { Integer age; public Biology() { } public Biology(Integer age) { this.age = age; System.out.println("生物类的构造器..."); } public void breath(){ } } package com.jsoft.morning; public class Animal extends Biology { String name; public Animal() { } public Animal(Integer age) { super(age); System.out.println("动物类的构造器..."); } public Animal(Integer age,String name) { super(age); // this(age); this.name = name; // this(name); } // public Animal(String name){ // this.name = name; // } public void eat(){ // 调用父类的属性 age = 20; // 调用父类的方法 breath(); // 调用当前类的属性 name = "z"; // 调用当前类的方法 // drank(); } public void drank(String water) { System.out.println("动物在喝" + water); } public String show() { return "我们很优秀"; } public Biology info(Biology biology) { return null; } } package com.jsoft.morning; public class Cat extends Animal { public Cat(Integer age) { super(age); } } package com.jsoft.morning; public class Person extends Animal { public Person() { } public Person(Integer age) { super(age); System.out.println("人类的构造器..."); } public void drank(String water){ System.out.println("人在喝" + water); System.out.println("喝完了" + water + "再来一杯!"); } public String show(){ // 返回的是我们很优秀,我们很直溜 return super.show() + "我们很直溜"; } public Animal info(Biology animal){ return null; } }
package com.jsoft.afternoon; public class Ch01 { private String name; private Integer age; /* this可以代表对象 super代表不了对象 */ public Ch01 info(String name,Integer age) { // Ch01 ch01 = new Ch01(); this.setName(name); this.setAge(age); show(this); return this; } public void show(Ch01 ch01) { } public static void main(String[] args) { Ch01 ch01 = new Ch01(); ch01 = ch01.info("abcd", 20); System.out.println(ch01.getName() + ch01.getAge()); // System.out.println(c.getName() + c.getAge()); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } package com.jsoft.afternoon; public class Person { private String name; private Integer age; public String getName() { return name; } public Person setName(String name) { this.name = name; return this; } public Integer getAge() { return age; } public Person setAge(Integer age) { this.age = age; return this; } } package com.jsoft.afternoon; public class Ch02 { public static void main(String[] args) { Person person = new Person(); // 链式处理 person.setName("张三").setAge(20); // person.setAge(20); } }
三、笔记内容


浙公网安备 33010602011771号