(写在开始)好记性不如烂笔头,为了巩固和验证自己所学内容,特写下这一系列文章,如有错误欢迎指正。
面向对象的三大特性
关于面向对象(Object-oriented programming),它是一种计算机编程架构,同时也是一种程序开发的方法。它将对象作为程序的基本单元,将程序和数据封装其中,这些基本单元相互独立相互调用形成我们的程序。而面向对象的三大特性:继承、多态、封装也是我这一节要记录的。
继承
继承的本质是在已有的类的基础上进行扩充或改造,得到新的方法,以满足新的需要。当一个类B能够获取另一个类A中所有非私有的成员属性和行为时,就称这两个类之间具有继承关系。被继承的类A称为父类或超类,继承了父类或超类的属性和行为的类B称为子类,其中Object
类为所有类的祖先,即所有类的父类。
优点:实现了代码复用
缺点:JAVA不允许使用多重继承,即一个子类不能同时继承多个父类。
下面是个例子:
Animal类
class Animal {
void eat() {
System.out.println("animal : eat");
}
}
class Dog extends Animal {
void eat() {
System.out.println("dog : eat");
}
void eatTest() {
this.eat(); // this 调用自己的方法
super.eat(); // super 调用父类方法
}
}
测试
public class Test {
public static void main(String[] args) {
Animal a = new Animal();
a.eat();
Dog d = new Dog();
d.eatTest();
}
}
输出结果
多态
首先,多态的实现有三个特征:
- 使用了继承;
- 父类中有虚方法(虚方法可以自行了解);
- 子类继承并实现了虚方法。
关于多态看到个很好的解释:
多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指向哪个类的实例对象,该引用变量发出的方法调用到底是哪个类中实现的方法,必须在由程序运行期间才能决定。因为在程序运行时才确定具体的类,这样,不用修改源程序代码,就可以让引用变量绑定到各种不同的类实现上,从而导致该引用调用的具体方法随之改变,即不修改程序代码就可以改变程序运行时所绑定的具体代码,让程序可以选择多个运行状态,这就是多态性。
可以看出多态是同一个事物具有多个不同表现形式或形态的能力。
优点:实现了可扩展性,使代码更灵活
缺点:在上下转型时易出错
下面是个例子:
动物类
class Animal {
int age;
String name;
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 void cry() {
System.out.println("animal:cry");
}
public void eat() {
System.out.println("animal:eat");
}
}
class Dog extends Animal {
public void cry() {
System.out.println("dog:cry");
}
public void eat() {
System.out.println("dog:eat");
}
}
class Cat extends Animal {
public void cry() {
System.out.println("cat:cry");
}
public void eat() {
System.out.println("cat:eat");
}
}
食物类
public class Food {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 食物类里面让它有一个方法
public void showName() {
System.out.println("food");
}
}
class Fish extends Food {
public void showName() {
System.out.println("food:fish");
}
}
class Bone extends Food {
public void showName() {
System.out.println("food:bone");
}
}
主人类
public class Master {
// 有了多态,以后即使再来好多动物,用这一个函数就可以了
public void feed(Animal an, Food f) {
an.eat();
f.showName();
}
}
测试
public class AnimalTest {
public static void main(String[] args) {
Master master = new Master();
master.feed(new Dog(), new Bone());
master.feed(new Cat(), new Fish());
}
}
输出结果
封装
封装,就像是一个穿着好看衣服的小姐姐,你能看到的只是表面的,却不知道里面是什么(也许还是个女装大佬)。封装将抽象性函数接口的实现细节包装起来,就像是加了一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访问,想要访问该类的代码和数据,则必须通过严格的接口控制。
优点:提高了安全性,减少耦合
缺点:为了预防用户的各种奇怪输入,要做更多的工作
下面是个例子:
// Multiple interfaces
interface CanFight {
void fight();
}
interface CanSwim {
void swim();
}
interface CanFly {
void fly();
}
class ActionCharacter {
public void fight(){
System.out.println("ActionCharacter:fight");}
}
class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly {
public void swim() {
System.out.println("Hero:Swim");
}
public void fly() {
System.out.println("Hero:fly");
}
public void fight() {
System.out.println("Hero:fight");
}
}
public class Adventure {
static void t(CanFight x) {
x.fight();
}
static void u(CanSwim x) {
x.swim();
}
static void v(CanFly x) {
x.fly();
}
static void w(ActionCharacter x) {
x.fight();
}
//测试
public static void main(String[] args) {
Hero h = new Hero();
t(h);
u(h);
v(h);
w(h);
}
}
输出结果
未完。。。