目录

第四章:封装、继承与多态

课件

下载:封装、继承与多态

面向对象的三大特性:封装

Circle1类

Circle1.java
package cn.edu.bjut.chapter4;
 
public class Circle1 {
	private double radius; 
 
	public Circle1(double radius) {
		this.radius = radius; 
	}
 
	public double getRadius() {
		return radius;
	}
 
	public void setRadius(double radius) {
		this.radius = radius;
	}
 
	public double area() {
		return (Math.PI * radius * radius); 
	}
 
	public double perimeter() {
		return (2 * Math.PI * radius); 
	}
}

Circle2类

Circle2.java
package cn.edu.bjut.chapter4;
 
public class Circle2 {
	private double diameter; 
 
	public Circle2(double radius) {
		this.diameter = 2 * radius; 
	}
 
	public double getRadius() {
		return (diameter / 2);
	}
 
	public void setRadius(double radius) {
		this.diameter = radius * 2;
	}
 
	public double area() {
		return (Math.PI * diameter * diameter / 4); 
	}
 
	public double perimeter() {
		return (Math.PI * diameter); 
	}
}

测试类:CircleTester类

CircleTester.java
package cn.edu.bjut.chapter4;
 
public class CircleTester {
	public static void main(String[] args) {
		Circle1 c1 = new Circle1(5); 
		Circle2 c2 = new Circle2(5); 
 
		System.out.println(c1.area() + "\t" + c2.area());
		System.out.println(c1.perimeter() + "\t" + c2.perimeter());
	}
}

继承:访问控制符

父类:Card类

Card.java
package cn.edu.bjut.chapter4;
 
public abstract class Card {
	protected String accountNumber; // 账号
	protected double balance; // 余额
 
	public Card(String accountNumber, double balance) {
		this.accountNumber = accountNumber;
		this.balance = (balance >= 0 ? balance : 0); 
	}
 
	public String getAccountNumber() {
		return this.accountNumber;
	}
 
	public void setAccountNumber(String accountNumber) {
		this.accountNumber = accountNumber;
	}
 
	public double getBalance() {
		return this.balance;
	}
 
	public boolean deposit(double money) {
		if (money <= 0) {
			return false; 
		}
 
		this.balance += money;
		return true;
	}
 
	public abstract boolean draw(double money); 
}

子类:CreditCard类

CreditCard.java
package cn.edu.bjut.chapter4;
 
public class CreditCard extends Card {
	private double overdraftLimit; // 透支额度
 
	public CreditCard(String accountNumber, double balance, 
			double overdraftLimit) {
		super(accountNumber, balance);
		this.overdraftLimit = overdraftLimit;
	}
 
	public CreditCard(String accountNumber, double overage) {
		this(accountNumber, overage, 0);
	}
 
	public double getOverdraftLimit() {
		return this.overdraftLimit;
	}
 
	public void setOverdraftLimit(double overdraftLimit) {
		this.overdraftLimit = (overdraftLimit >= 0 ? overdraftLimit : 0);
	}
 
	@Override
	public boolean draw(double money) {
		if (balance + overdraftLimit < money) {
			return false;
		}
 
		balance -= money;
		return true;
	}
}

子类:DebitCard类

DebitCard.java
package cn.edu.bjut.chapter4;
 
public class DebitCard extends Card {
	public DebitCard(String accountNumber, double balance) {
		super(accountNumber, balance);
	}
 
	@Override
	public boolean draw(double money) {
		if (balance < money) {
			return false;
		}
 
		balance -= money;
		return true;
	}
}

测试类:CardTester类

CardTester.java
package cn.edu.bjut.chapter4;
 
public class CardTester {
	public static void main(String[] args) {
		System.out.println("------------  借记卡账户  ------------");
		Card debitCard = new DebitCard("CHK20100117001", 100);
		System.out.println("取90元的结果:" + debitCard.draw(90));
		debitCard.deposit(90);
		System.out.println("取120元的结果:" + debitCard.draw(120));
 
		System.out.println("------------  信用卡账户  ------------");
		Card crebitCard = new CreditCard("CHK20100117002", 100, 50);
		System.out.println("取90元的结果:" + crebitCard.draw(90));
		crebitCard.deposit(90);
		System.out.println("取120元的结果:" + crebitCard.draw(120));
		crebitCard.deposit(120);
		System.out.println("取160元的结果:" + crebitCard.draw(160));
	}
}

继承中的成员方法覆盖

父类:Rectangle类

Rectangle.java
package cn.edu.bjut.chapter4;
 
public class Rectangle {
	protected int length, width;
 
	public Rectangle(int length, int width) {
		this.length = length;
		this.width = width;
	}
	public int area() {
		System.out.println(this.getClass().getName());
		return (length * width);
	}
	public int perimeter() {
		System.out.println(this.getClass().getName());
		return (2 * (length + width));
	}
}

子类:Square类

Square.java
package cn.edu.bjut.chapter4;
 
public class Square extends Rectangle {
	public Square(int length) {
		super(length, length); 
	}
 
	public int area() {
		System.out.println(this.getClass().getName());
		return (length * length); 
	}
 
	public int perimeter() {
		System.out.println(this.getClass().getName());
		return (length << 2); 
	}
}

测试类:SquareTester类

SquareTester.java
package cn.edu.bjut.chapter4;
 
public class SquareTester {
	public static void main(String[] args) {
		Rectangle rect = new Square(6); 
 
		System.out.println(rect.area());
		System.out.println(rect.perimeter());
	}
}

继承中的数据成员隐藏

PropertyHiddenTester.java
package cn.edu.bjut.chapter4;
 
class ClassA {
	int a = 20; 
}
 
class ClassB extends ClassA {
	int a = 30; 
	public void print() {
		System.out.println(a + "\t" + super.a);
	}
}
 
public class PropertyHiddenTester {
	public static void main(String[] args) {
		ClassB classB = new ClassB(); 
		classB.print();
	}
}

父类引用强转为子类引用

父类:Person类

Person.java
package cn.edu.bjut.chapter4;
 
public class Person {
	protected String id; //身份证号
	protected String name; // 姓名
	protected char gender; // 性别
 
	public Person(String id, String name, char gender) {
		this.id = id; 
		this.name = name; 
		this.gender = gender; 
	}
 
	public String getId() {
		return id;
	}
 
	public void setId(String id) {
		this.id = id;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public char getGender() {
		return gender;
	}
 
	public void setGender(char gender) {
		this.gender = gender;
	}
}

子类:Student类

Student.java
package cn.edu.bjut.chapter4;
 
public class Student extends Person {
	private String studentId; // 学号
	private String classNo;  //班级
 
	public Student(String id, String name, char gender, 
			String studentId, String classNo) {
		super(id, name, gender);
 
		this.studentId = studentId; 
		this.classNo = classNo; 
	}
 
	public String getStudentId() {
		return studentId;
	}
 
	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}
 
	public String getClassNo() {
		return classNo;
	}
 
	public void setClassNo(String classNo) {
		this.classNo = classNo;
	}
}

子类:Teacher类

Teacher.java
package cn.edu.bjut.chapter4;
 
public class Teacher extends Person {
	private String teacherId; // 职工号
	private String title; //职称
 
	public Teacher(String id, String name, char gender, 
			String teacherId, String title) {
		super(id, name, gender); 
		this.teacherId = teacherId; 
		this.title = title; 
	}
	public String getTeacherId() {
		return teacherId;
	}
	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
}

测试类:PersonTester类

PersonTester.java
package cn.edu.bjut.chapter4;
 
public class PersonTester {
	public static void main(String[] args) {
		Person p1 = new Student("123456789", "李文慧", '女', "052615", "05"); 
		Person p2 = new Teacher("987654321", "梁化祥", '男', "071235", "教授"); 
 
		System.out.println(p1.getId() + "\t" + p1.getName() + "\t" + p1.getGender());
		//System.out.println(p1.getStudentId() + "\t" + p1.getClassNo());
		System.out.println(p2.getId() + "\t" + p2.getName() + "\t" + p2.getGender());
		//System.out.println(p2.getTeacherId() + "\t" + p2.getTitle());
 
		if (p1 instanceof Student) {
			Student stu = (Student) p1; 
			System.out.println(stu.getStudentId() + "\t" + stu.getClassNo());
		}
 
		if (p2 instanceof Teacher) {
			Teacher tea = (Teacher) p2; 
			System.out.println(tea.getTeacherId() + "\t" + tea.getTitle());
		}
	}
}

抽象类与抽象方法

抽象父类:Shape类

Shape.java
package cn.edu.bjut.chapter4_1;
 
public abstract class Shape {
	public abstract double area(); 
	public abstract double perimeter(); 
}

子类:Rectangle类

Rectangle.java
package cn.edu.bjut.chapter4_1;
 
public class Rectangle extends Shape {
	private double length, width; 
 
	public Rectangle(double length, double width) {
		this.length = length; 
		this.width = width; 
	}
 
	@Override
	public double area() {
		return (length * width);
	}
 
	@Override
	public double perimeter() {
		return (2 * (length + width));
	}
 
}

子类:Circle类

Circle.java
package cn.edu.bjut.chapter4_1;
 
public class Circle extends Shape {
	private double radius; 
 
	public Circle(double radius) {
		this.radius = radius; 
	}
 
	@Override
	public double area() {
		return (Math.PI * radius * radius);
	}
 
	@Override
	public double perimeter() {
		return (2 * Math.PI * radius);
	}
}

子类:Triangle类

Triangle.java
package cn.edu.bjut.chapter4_1;
 
public class Triangle extends Shape {
	private double a, b, c; 
 
	public Triangle(double a, double b, double c) {
		this.a = a; 
		this.b = b; 
		this.c = c; 
	}
 
	@Override
	public double area() {
		double p = (a + b + c) / 2; 
 
		return Math.sqrt(p * (p - a) * (p - b) * (p - c)); 
	}
 
	@Override
	public double perimeter() {
		return (a + b + c);
	}
}

接口

抽象父类:Animal

Animal.java
package cn.edu.bjut.chapter4;
 
public abstract class Animal {
	protected String name; 
 
	public Animal(String name) {
		this.name = name; 
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public abstract String makeNoise(); 
}

接口:Runable

Runable.java
package cn.edu.bjut.chapter4;
 
public interface Runable {
	String UNIT = "千米/小时"; 
 
	double getSpeed(); 
}

接口:Flyable

Flyable.java
package cn.edu.bjut.chapter4;
 
public interface Flyable {
	String UNIT = "千米"; 
 
	double getFlightHeight(); 
}

子类:Dog

Dog.java
package cn.edu.bjut.chapter4;
 
public class Dog extends Animal implements Runable {
	private double speed; 
 
	public Dog(String name, double speed) {
		super(name);
		this.speed = speed; 
	}
 
	@Override
	public String makeNoise() {
		return "汪!汪!汪!";
	}
 
	@Override
	public double getSpeed() {
		return speed;
	}
 
	@Override
	public String toString() {
		return "Dog [奔跑速度:" + speed + Runable.UNIT + "]";
	}
}

子类:Pigeon

Pigeon.java
package cn.edu.bjut.chapter4;
 
public class Pigeon extends Animal implements Flyable, Runable {
	private double flightHeight; 
	private double speed; 
 
	public Pigeon(String name, double flightHeight, double speed) {
		super(name);
		this.flightHeight = flightHeight; 
		this.speed = speed; 
	}
 
	@Override
	public double getFlightHeight() {
		return flightHeight;
	}
	@Override
	public String makeNoise() {
		return "咕!咕!咕!";
	}
	@Override
	public double getSpeed() {
		return speed; 
	}
	@Override
	public String toString() {
		return "Pigeon [飞行高度:" + flightHeight + Flyable.UNIT 
				+ ", 奔跑速度:" + speed + Runable.UNIT + "]";
	}
}

测试类:AnimalTester.java

AnimalTester.java
package cn.edu.bjut.chapter4;
 
public class AnimalTester {
	public static void main(String[] args) {
		Dog dog = new Dog("小毛绒", 20); 
		Pigeon pigeon = new Pigeon("捣蛋鬼", 0.5, 5); 
 
		System.out.println(dog);
		System.out.println(pigeon); 
	}
}

Java8新特性:default与static

InterfaceNewFeatureTester.java
package cn.edu.bjut.chapter4;
 
interface Drawable {
	void draw(); 
 
	default void msg() {
		System.out.println("default method");
	}
 
	static int cube (int x) {
		return (x * x * x); 
	}
}
 
class Circle implements Drawable {
	@Override
	public void draw() {
		System.out.println("drawing circle");
	}
}
 
public class InterfaceNewFeatureTester {
	public static void main(String[] args) {
		Drawable d = new Circle(); 
		d.draw();
		d.msg(); 
		System.out.println(Drawable.cube(3));
	}
}

上机作业

【参考答案】

Classification.java
package cn.edu.bjut.chapter4;
 
public class Classification {
	protected char section;
	protected int mainClass;
	protected char subclass;
	protected int mainGroup;
	protected int subgroup;
 
	public final static char SEPARATOR = '/';
 
	public Classification(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
		this.section = section;
		this.mainClass = mainClass;
		this.subclass = subclass;
		this.mainGroup = mainGroup;
		this.subgroup = subgroup;
	}
 
	// example: G09F 17/30
	public Classification(String symbol) {
		this.section = symbol.charAt(0);
		this.mainClass = Integer.parseInt(symbol.substring(1, 3));
		this.subclass = symbol.charAt(3);
 
		int pos = symbol.indexOf(SEPARATOR);
		this.mainGroup = Integer.parseInt(symbol.substring(4, pos).trim());
		this.subgroup = Integer.parseInt(symbol.substring(pos + 1));
	}
 
	public char getSection() {
		return section;
	}
 
	public void setSection(char section) {
		this.section = section;
	}
 
	public int getMainClass() {
		return mainClass;
	}
 
	public void setMainClass(int mainClass) {
		this.mainClass = mainClass;
	}
 
	public char getSubclass() {
		return subclass;
	}
 
	public void setSubclass(char subclass) {
		this.subclass = subclass;
	}
 
	public int getMainGroup() {
		return mainGroup;
	}
 
	public void setMainGroup(int mainGroup) {
		this.mainGroup = mainGroup;
	}
 
	public int getSubgroup() {
		return subgroup;
	}
 
	public void setSubgroup(int subgroup) {
		this.subgroup = subgroup;
	}
 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
 
		sb.append(section);
 
		String strMainClass = String.format("%2s", mainClass).replace(' ', '0');
		sb.append(strMainClass);
 
		sb.append(subclass);
 
		String strMainGroup = String.format("%4s", mainGroup);
		sb.append(strMainGroup);
 
		sb.append(SEPARATOR);
 
		sb.append(subgroup);
 
		return sb.toString();
	}
}
Ipc.java
package cn.edu.bjut.chapter4;
 
public class Ipc extends Classification {
 
	public Ipc(char section, int mainClass, char subclass, int mainGroup, int subgroup) {
		super(section, mainClass, subclass, mainGroup, subgroup); 
	}
 
	public Ipc(String symbol) {
		super(symbol); 
	}
 
	public static void main(String[] args) {
		char section = 'A'; 
		int mainClass = 4; 
		char subclass = 'B'; 
		int mainGroup = 55; 
		int subgroup = 56; 
 
		Classification ipc = new Ipc(section, mainClass, subclass, mainGroup, subgroup); 
		System.out.println(ipc); 
 
		String symbol = "G09F 17/30"; 
		System.out.println(new Ipc(symbol)); 
	}
}
Cpc.java
package cn.edu.bjut.chapter4;
 
public class Cpc extends Classification {
	private char position; 
	private char value; 
 
	public Cpc(char section, int mainClass, char subclass, int mainGroup, int subgroup, char position, char value) {
		super(section, mainClass, subclass, mainGroup, subgroup); 
 
		this.position = position; 
		this.value = value; 
	}
 
	public Cpc(String symbol, char position, char value) {
		super(symbol); 
 
		this.position = position; 
		this.value = value; 
	}
 
	public char getPosition() {
		return position;
	}
 
	public void setPosition(char position) {
		this.position = position;
	}
 
	public char getValue() {
		return value;
	}
 
	public void setValue(char value) {
		this.value = value;
	}
}

返回Java课程页