文章目录
- 第四章 面向对象(中)
- 继承性的使用与理解
- 继承性练习
- 方法的重写(override/overwrite)
- 方法重写的细节
- 方法的练习
- 四种访问权限修饰符
- 关键字:super
- 子类对象实例化过程(`尽量理解!`!)
- 面向对象特征之三:多态性
- 虚拟方法的补充
第四章 面向对象(中)
原文:https://shimo.im/docs/DhhTyxtgwdpjVd39/ 《第四章 面向对象(中)》
继承性的使用与理解
- Person 类
public class Person {
String name;
private int age;
public Person(){
}
public Person(String name,int age){
this.name = name;
this.age = age;
}
public void eat(){
System.out.println("吃饭");
sleep();
}
private void sleep(){
System.out.println("睡觉");
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
- Student 类
public class Student extends Person {
// String name;
// int age;
String major;
public Student(){
}
public Student(String name,int age,String major){
this.name = name;
// this.age = age;
setAge(age);
this.major = major;
}
// public void eat(){
// System.out.println("吃饭");
// }
//
// public void sleep(){
// System.out.println("睡觉");
// }
public void study(){
System.out.println("学习");
}
public void show(){
System.out.println("name:" + name + ",age = " + getAge());
}
}
- 测试类
public class ExtendsTest {
public static void main(String[] args) {
Person p1 = new Person();
// p1.age = 1;
p1.eat();
System.out.println("********************");
Student s1 = new Student();
s1.eat();
// s1.sleep();
s1.name = "Tom";
s1.setAge(10);
System.out.println(s1.getAge());
}
}
- Java 中关于继承性的规定
public class ExtendsTest {
public static void main(String[] args) {
s1.brease();
Creature c = new Creature();
System.out.println(c.toString());
}
}
- 将上述 Person 类改为如下
public class Person extends Creature {
...
}
- Creature 类
public class Creature {
public void brease(){
System.out.println("呼吸");
}
}
继承性练习
- 练习1
public class Kids extends ManKind{
private int yearsOld; //年限
public Kids() {
}
public Kids(int yearsOld) {
this.yearsOld = yearsOld;
}
public int getYearsOld() {
return yearsOld;
}
public void setYearsOld(int yearsOld) {
this.yearsOld = yearsOld;
}
public void printAge(){
System.out.println("I am " + yearsOld);
}
}
- ManKind类
public class ManKind {
private int sex; //性别
private int salary; //薪资
public ManKind() {
}
public ManKind(int sex, int salary) {
this.sex = sex;
this.salary = salary;
}
public void manOrWoman(){
if(sex==1){
System.out.println("man");
}else if(sex==0){
System.out.println("woman");
}
}
public void employeed(){
if(salary==0){
System.out.println("no job");
}else if(salary!=0){
System.out.println("job");
}
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
- KidsTest
public class KidsTest {
public static void main(String[] args) {
Kids someKid = new Kids(12);
someKid.printAge();
someKid.setYearsOld(15);
someKid.setSalary(0);
someKid.setSex(1);
someKid.employeed();
someKid.manOrWoman();
}
}
- 练习2
public class Circle {
public double radius; //半径
public Circle(){
radius = 1.0;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea(){ //计算圆的面积
return Math.PI * radius * radius;
}
}
- Cylinder类
public class Cylinder extends Circle{
private double length;
public Cylinder(){
length = 1.0;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double findVolume(){ //计算圆柱体积
return findArea() * length;
}
}
- 测试类
public class CylinderTest {
public static void main(String[] args) {
Cylinder cy = new Cylinder();
cy.setRadius(2.1);
cy.setLength(3.4);
double volues = cy.findVolume();
System.out.println("圆柱的体积:" + volues);
double area = cy.findArea();
System.out.println("圆的面积: " + area);
}
}
方法的重写(override/overwrite)
- Person类
public class Person {
String name;
int age;
public Person(){
}
public Person(String name,int age){
this.name = name;
this.age = age;
}
public void eat(){
System.out.println("吃饭");
}
public void walk(int distance){
System.out.println("走路,走的距离是:" + distance + "公里");
show();
}
private void show(){
System.out.println("我是一个人。");
}
public Object info(){
return null;
}
public double info1(){
return 1.0;
}
}
- Student类
public class Student extends Person{
String major;
public Student(){
}
public Student(String major){
this.major = major;
}
public void study(){
System.out.println("学习,专业是:" + major);
}
//对父类中的eat()进行了重写
public void eat(){
System.out.println("学生应该多吃有营养的。");
}
public void show(){
System.out.println("我是一个学生。");
}
public String info(){
return null;
}
//不是一个类型,所以报错。
// public int info1(){
// return 1;
// }
//可以直接将父类的方法的第一行粘过来,直接写方法体
// public void walk(int distance){
// System.out.println("重写的方法");
// }
//直接输入父类的方法名,Alt + /,选择即可生成
@Override
public void walk(int distance) {
System.out.println("自动生成");
}
}
- 测试类
public class PersonTest {
public static void main(String[] args) {
Student s = new Student("计算机科学与技术");
s.eat();
s.walk(10);
s.study();
}
}
方法重写的细节
- Person类
public class Person {
String name;
int age;
public Person(){
}
public Person(String name,int age){
this.name = name;
this.age = age;
}
// public void eat(){
// System.out.println("吃饭");
// }
static void eat(){
System.out.println("吃饭");
}
public void walk(int distance){
System.out.println("走路,走的距离是:" + distance + "公里");
show();
}
private void show(){
System.out.println("我是一个人。");
}
public Object info(){
return null;
}
public double info1(){
return 1.0;
}
}
- Student类
public class Student extends Person{
String major;
public Student(){
}
public Student(String major){
this.major = major;
}
public void study(){
System.out.println("学习,专业是:" + major);
}
//对父类中的eat()进行了重写
// public void eat(){
// System.out.println("学生应该多吃有营养的。");
// }
//这样不会报错,但已经不是重写了!!
public static void eat(){
System.out.println("学生应该多吃有营养的。");
}
public void show(){
System.out.println("我是一个学生。");
}
public String info(){
return null;
}
//不是一个类型,所以报错。
// public int info1(){
// return 1;
// }
//可以直接将父类的方法的第一行粘过来,直接写方法体
// public void walk(int distance){
// System.out.println("重写的方法");
// }
//直接输入父类的方法名,Alt + /,选择即可生成
@Override
public void walk(int distance) {
System.out.println("自动生成");
}
}
- 测试类
public class PersonTest {
public static void main(String[] args) {
Student s = new Student("计算机科学与技术");
s.eat();
s.walk(10);
System.out.println("*******************");
s.study();
Person p1 = new Person();
p1.eat();
}
}
方法的练习
- 练习1
1.如果现在父类的一个方法定义成private访问权限,在子类中将此方法声明为default访问权限,那么这样还叫重写吗?(NO)
- 练习2
public class Kids extends ManKind{
private int yearsOld; //年限
public Kids() {
}
public Kids(int yearsOld) {
this.yearsOld = yearsOld;
}
public int getYearsOld() {
return yearsOld;
}
public void setYearsOld(int yearsOld) {
this.yearsOld = yearsOld;
}
public void printAge(){
System.out.println("I am " + yearsOld);
}
public void employeed(){
System.out.println("Kids should study and no job.");
}
}
- MindKids类
public class ManKind {
private int sex; //性别
private int salary; //薪资
public ManKind() {
}
public ManKind(int sex, int salary) {
this.sex = sex;
this.salary = salary;
}
public void manOrWoman(){
if(sex==1){
System.out.println("man");
}else if(sex==0){
System.out.println("woman");
}
}
public void employeed(){
if(salary==0){
System.out.println("no job");
}else if(salary!=0){
System.out.println("job");
}
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
- 测试类
public class KidsTest {
public static void main(String[] args) {
Kids someKid = new Kids(12);
someKid.printAge();
someKid.setYearsOld(15);
someKid.setSalary(0);
someKid.setSex(1);
someKid.employeed();
someKid.manOrWoman();
}
}
四种访问权限修饰符
对于之前的四种权限修饰符的补充
- Order类
package githubb;
public class Order {
private int orderPrivate;
int orderDefault;
protected int orderProtected;
public int orderPublic;
private void methodPrivate(){
orderPrivate = 1;
orderDefault = 2;
orderProtected = 3;
orderPublic = 4;
}
void methodDefault(){
orderPrivate = 1;
orderDefault = 2;
orderProtected = 3;
orderPublic = 4;
}
protected void methodProtected(){
orderPrivate = 1;
orderDefault = 2;
orderProtected = 3;
orderPublic = 4;
}
public void methodPublic(){
orderPrivate = 1;
orderDefault = 2;
orderProtected = 3;
orderPublic = 4;
}
}
- Ordertest类
package githubb;
public class OrderTest {
public static void main(String[] args) {
Order order = new Order();
order.orderDefault = 1;
order.orderProtected = 2;
order.orderPublic = 3;
order.methodDefault();
order.methodProtected();
order.methodPublic();
//同一个包中的其它类,不可以调用Order类中私有的属性
// order.orderPrivate = 4; //The field Order.orderPrivate is not visible
// order.methoPrivate();
}
}
- SubOrder类
package githubc;
import githubb.Order;
public class SubOrder extends Order{
public void method(){
orderProtected = 1;
orderPublic = 2;
methodProtected();
methodPublic();
//在不同包的子类中,不能调用Order类中声明为private和缺省的权限的属性、方法
// orderDefault = 3;
// orderPrivate = 4;
//
// methodDefault();
// methodPrivate();
}
}
- OrderTest类
package githubc;
import githubb.Order;
public class OrderTest {
public static void main(String[] args) {
Order order = new Order();
order.orderPublic = 1;
order.methodPublic();
//不同包下的普通类(非子类)要使用Order类,不可以调用声明为private、缺省、protected权限的属性、方法。
// order.orderPrivate = 2;
// order.orderProtected = 3;
// order.orderProtected = 4;
//
// order.methodPrivate();
// order.methodDefault();
// order.methodProtected();
}
public void show(Order order){
order.orderPublic = 1;
order.methodPublic();
//不同包下的普通类(非子类)要使用Order类,不可以调用声明为private、缺省、protected权限的属性、方法。
// order.orderPrivate = 2;
// order.orderProtected = 3;
// order.orderProtected = 4;
//
// order.methodPrivate();
// order.methodDefault();
// order.methodProtected();
}
}
关键字:super
- Person类
public class Person {
String name;
int age;
int id = 1003; //身份证号
public Person(){
System.out.println("我无处不在");
}
public Person(String name){
this.name = name;
}
public Person(String name,int age){
this(name);
this.age = age;
}
public void eat(){
System.out.println("人,吃饭");
}
public void walk(){
System.out.println("人,走路");
}
}
- Student类
public class Student extends Person{
String major;
int id = 1002; //学号
public Student(){
}
public Student(String name,int age,String major){
// this.age = age;
// this.name = name;
super(name,age);
this.major = major;
}
public Student(String major){
this.major = major;
}
public void eat(){
System.out.println("学生多吃有营养的食物");
}
public void Study(){
System.out.println("学生,学习知识。");
this.eat();
//如果,想调用父类中被重写的,不想调用子类中的方法,可以:
super.eat();
super.walk();//子父类中未重写的方法,用"this."或"super."调用都可以
}
public void show(){
System.out.println("name = " + this.name + ",age = " + super.age);
System.out.println("id = " + this.id);
System.out.println("id = " + super.id);
}
}
- 测试类
public class SuperTest {
public static void main(String[] args) {
Student s = new Student();
s.show();
s.Study();
Student s1 = new Student("Ton",21,"IT" );
s1.show();
System.out.println("***********************");
Student s2 = new Student();
}
}
子类对象实例化过程(尽量理解!
!)
public class InstanceTest {
}
附件:实验继承-super.pdf
- Account类
public class Account {
private int id; //账号
private double balance; //余额
private double annualInterestRate; //年利率
public Account(int id, double balance, double annualInterestRate) {
super();
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterest(){ //返回月利率的方法
return annualInterestRate / 12;
}
public void withdraw (double amount){ //取款方法
if(balance >= amount){
balance -= amount;
return;
}
System.out.println("余额不足");
}
public void deposit (double amount){ //存款方法
if(amount > 0){
balance += amount;
}
}
}
- AccountTest类
public class AccountTest {
public static void main(String[] args) {
Account acct = new Account(1122,20000,0.045);
acct.withdraw(30000);
System.out.println("你的账户余额为:" + acct.getBalance());
acct.withdraw(2500);
System.out.println("你的账户余额为:" + acct.getBalance());
acct.deposit(3000);
System.out.println("你的账户余额为:" + acct.getBalance());
System.out.println("月利率为: " + (acct.getAnnualInterestRate() * 100) + "%");
}
}
- CheckAccount类
public class CheckAccount extends Account{
private double overdraft; //代表可透支限额
public CheckAccount(int id, double balance, double annualInterestRate,double overdraft){
super(id, balance, annualInterestRate);
this.overdraft = overdraft;
}
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
@Override
public void withdraw(double amount) {
if(getBalance() >= amount){ //余额足够消费
//方式一
// setBalance(getBalance() - amount);
//方式二
super.withdraw(amount);
}else if(overdraft >= amount - getBalance()){ //余额不够
overdraft -= (amount - getBalance());
// setBalance(0);
//或
super.withdraw(getBalance());
}else{ //超过可透支限额
System.out.println("超过可透支限额!");
}
}
}
- CheckAccountTest类
public class CheckAccountTest {
public static void main(String[] args) {
CheckAccount cat = new CheckAccount(1122,20000,0.045,5000);
cat.withdraw(5000);
System.out.println("您的账户余额为: " + cat.getBalance());
System.out.println("您的可透支额度为: " + cat.getOverdraft());
cat.withdraw(18000);
System.out.println("您的账户余额为: " + cat.getBalance());
System.out.println("您的可透支额度为: " + cat.getOverdraft());
cat.withdraw(3000);
System.out.println("您的账户余额为: " + cat.getBalance());
System.out.println("您的可透支额度为: " + cat.getOverdraft());
}
}
面向对象特征之三:多态性
- Person类
public class Person {
String name;
int age;
public void eat(){
System.out.println("人,吃饭");
}
public void walk(){
System.out.println("人,走路");
}
}
- Woman类
public class Woman extends Person{
boolean isBeauty;
public void goShopping(){
System.out.println("女人喜欢购物");
}
public void eat(){
System.out.println("女人少吃,为了减肥。");
}
public void walk(){
System.out.println("女人,窈窕的走路。");
}
}
- Man类
public class Man extends Person{
boolean isSmoking;
public void earnMoney(){
System.out.println("男人负责工作养家");
}
public void eat() {
System.out.println("男人多吃肉,长肌肉");
}
public void walk() {
System.out.println("男人霸气的走路");
}
}
- 测试类
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.eat();
Man man = new Man();
man.eat();
man.age = 25;
man.earnMoney();
/
public class AnimalTest {
public static void main(String[] args) {
AnimalTest test = new AnimalTest();
test.func(new Dog());
test.func(new Cat());
}
public void func(Animal animal){ //Animal animal = new Dog();
animal.eat();
animal.shout();
}
//如果没有多态性,就会写很多如下的方法,去调用
public void func(Dog dog){
dog.eat();
dog.shout();
}
public void func(Cat cat){
cat.eat();
cat.shout();
}
}
class Animal{
public void eat(){
System.out.println("动物,进食");
}
public void shout(){
System.out.println("动物:叫");
}
}
class Dog extends Animal{
public void eat(){
System.out.println("狗吃骨头");
}
public void shout() {
System.out.println("汪!汪!汪!");
}
}
class Cat extends Animal{
public void eat(){
System.out.println("猫吃鱼");
}
public void shout() {
System.out.println("喵!喵!喵!");
}
}
虚拟方法的补充
import java.util.Random;
//面试题:多态是编译时行为还是运行时行为?
//证明如下:
class Animal {
protected void eat() {
System.out.println("animal eat food");
}
}
class Cat extends Animal {
protected void eat() {
System.out.println("cat eat fish");
}
}
class Dog extends Animal {
public void eat() {
System.out.println("Dog eat bone");
}
}
class Sheep extends Animal {
public void eat() {
System.out.println("Sheep eat grass");
}
}
public class InterviewTest {
public static Animal getInstance(int key) {
switch (key) {
case 0:
return new Cat ();
case 1:
return new Dog ();
default:
return new Sheep ();
}
}
public static void main(String[] args) {
int key = new Random().nextInt(3);
System.out.println(key);
Animal animal = getInstance(key);
animal.eat();
}
}
整个Java全栈系列都是笔者自己敲的笔记。写作不易,如果可以,点个赞呗!