JavaJava学习 - day002
WiretenderJava 学习笔记
对象的成员方法
当程序执行到方法时,就会开辟一个独立的空间(栈空间)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package javahsp.day04;
public class Method01 { public static void main(String[] args) { Person wangXinNuo = new Person(); wangXinNuo.speak(); wangXinNuo.cal01(); wangXinNuo.cal02(10); } }
class Person { String name; int age;
public void speak() { System.out.println("我是一个好人"); } public void cal01() { int res = 0; for (int i = 1;i <= 100;i ++) { res += i; } System.out.println("计算结果 = " + res); } public void cal02(int end) { int res = 0; for (int i = 1;i <= end;i ++) { res += i; } System.out.println(" 计算结果 = " + res); } public int getSum(int num1, int num2) { int res = num1 + num2; return res; } }
|
递归
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package javahsp.day04;
public class RecursionExercise01 { public static void main(String[] args) { T t1 = new T(); System.out.println("斐波那契数字:" + t1.fibonacci(8)); } }
class T { public int fibonacci(int n) { if (n >= 1) { if (n == 1 || n == 2) { return 1; } else { return fibonacci(n-1) + fibonacci(n-2); } } else { System.out.println("要求输入 >= 1的正整数"); return -1; } } }
|
继承
子类继承了所有的属性和方法,非私有的属性和方法可以在子类直接访问, 但是私有属性和方法不能在子类直接访 问,要通过父类提供公共的方法去访问
子类必须调用父类的构造器,完成父类的初始化
当创建子类对象时,不管使用子类的哪个构造器,默认情况下总会去调用父类的无参构造器,如果父类没有提供无 参构造器,则必须在子类的构造器中用 super 去指定使用父类的哪个构造器完成对父类的初始化工作,否则,编译不会通过(怎么理解。)[举例说明]
4.如果希望指定去调用父类的某个构造器,则显式的调用一下: super(参数列表)
super 在使用时,必须放在构造器第一行(super 只能在构造器中使用)
super()和 this()都只能放在构造器第一行,因此这两个方法不能共存在一个构造器
java 所有类都是 Object 类的子类, Object 是所有类的基类.
父类构造器的调用不限于直接父类!将一直往上追溯直到 Object 类(顶级父类)
子类最多只能继承一个父类(指直接继承),即 java 中是单继承机制。
思考:如何让 A 类继承 B 类和 C 类?【A 继承 B,B 继承 C】
不能滥用继承,子类和父类之间必须满足 is-a 的逻辑关系
练习
1 2 3 4 5 6 7 8 9
| package com.hspedu._extend;
public class ExtendsExercise03 { public static void main(String[] args) { PC pc = new PC("intel", 16, 500, "IBM"); pc.printInfo(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.hspedu._extend;
public class Computer { private String cpu; private int memory; private int disk;
public Computer() {
}
public Computer(String cpu, int memory, int disk) { this.cpu = cpu; this.memory = memory; this.disk = disk; }
public String getDetails() { return " cpu=" + cpu + " memory=" + memory + " disk=" + disk; }
public int getMemory() { return memory; }
public void setMemory(int memory) { this.memory = memory; }
public String getCpu() { return cpu; }
public void setCpu(String cpu) { this.cpu = cpu; }
public int getDisk() { return disk; }
public void setDisk(int disk) { this.disk = disk; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.hspedu._extend;
public class PC extends Computer { private String brand; public PC(String cpu, int memory, int disk, String brand) { super(cpu, memory, disk); this.brand = brand; }
public String getBrand() { return brand; }
public void setBrand(String brand) { this.brand = brand; }
public void printInfo() { System.out.println("PC信息 = ");
System.out.println(getDetails() + " brand = " + brand); }
}
|
多态
- 多态就是为了解决代码的重复问题,我们通过向下转型和向上转型的两种方法来实现面向对象中的多态。
多态的应用-多态数组
1 2 3 4
| if (person[i] instanceof Student) { Student student = (Student)person[i]; student.study(); }
|
多态参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.hspedu.poly_.polyparameter;
public class PloyParameter { public static void main(String[] args) { Worker tom = new Worker(2500,"tom"); Manager milan = new Manager(5000, "milan", 250000); PloyParameter ployParameter = new PloyParameter(); ployParameter.showEmpAnnual(tom); ployParameter.showEmpAnnual(milan); ployParameter.testWork(tom); ployParameter.testWork(milan); }
public void showEmpAnnual(Employee e) { System.out.println(e.getAnnual()); }
public void testWork(Employee e) { if (e instanceof Worker) { ((Worker) e).work(); } else if (e instanceof Employee) { ((Manager) e).manage(); } else { System.out.println("不做处理 ..."); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.hspedu.poly_.polyparameter;
public class Employee { private String name; private double salary;
public Employee(double salary, String name) { this.salary = salary; this.name = name; } public double getAnnual() { return 12 * salary; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.hspedu.poly_.polyparameter;
public class Worker extends Employee { public Worker(double salary, String name) { super(salary, name); } public void work() { System.out.println("员工" + getName() + "is working"); }
@Override public double getAnnual() { return super.getAnnual(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.hspedu.poly_.polyparameter;
public class Manager extends Employee { private double bonus;
public Manager(double salary, String name, double bonus) { super(salary, name); this.bonus = bonus; }
public void manage() { System.out.println("经理 " + getName() + "is managing"); } public double getBonus() { return bonus; }
public void setBonus(double bonus) { this.bonus = bonus; }
@Override public double getAnnual() { return super.getAnnual() + bonus; } }
|