Java面向對象編程四大特徵
抽象性:抽出事物的本質特性,把某類事物的共同的特點描述出來;
封裝性:把對象的屬性和操作結合在一起,構成一個獨立的封裝體
繼承性:使得一個類可以繼承另一個類的屬性和方法
多態性:指不同類型的對象接收相同的消息時產生不同的行爲
方法重載(overload)和方法覆蓋(override)
一、方法重載
方法重載就是類的同一種功能的多種實現方式,到底採用哪種方式,取決於調用者給出的參數。
注意事項:
- 方法名相同
- 方法的參數類型、個數、順序至少有一項不同
- 方法返回類型可以不同
- 方法的修飾符可以不同
只有返回類型不一樣,不能構成重載
代碼示例:
package com.xx.day8;
/**
* @author 程序員小新兒
*2020年4月7日
*/
class Calculation{
//接收兩個整數,返回較大的數
public int getMax(int x , int y) {
if (x > y) {
return x;
}else {
return y;
}
}
//接收兩個float型的數,返回較大的數
public float getMaxFloat(float a , float b) {
if (a > b) {
return a;
}else {
return b;
}
}
}
public class OverLoad {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculation calculation = new Calculation();
//比較整數大小
System.out.println(calculation.getMax(1, 2));
//比較float數大小
System.out.println(calculation.getMaxFloat(2.1f, 3.4f));
}
}
方法重載後:
package com.xx.day8;
/**
* @author 程序員小新兒
*2020年4月7日
*/
class Calculation{
//接收兩個整數,返回較大的數
public int getMax(int x , int y) {
if (x > y) {
return x;
}else {
return y;
}
}
//接收兩個float型的數,返回較大的數
public float getMax(float a , float b) {
if (a > b) {
return a;
}else {
return b;
}
}
}
public class OverLoad {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculation calculation = new Calculation();
//比較整數大小
System.out.println(calculation.getMax(1, 2));
//比較float數大小
System.out.println(calculation.getMax(2.1f, 3.4f));
}
}
二、方法覆蓋
我們經常叫方法重寫
方法覆蓋就是子類有一個方法,和父類的某個方法的名稱、返回類型、參數一樣,那麼我們就說子類的這個方法覆蓋了父類的那個方法。
注意事項:
- 子類的方法的返回類型、參數、方法名稱,要和父類方法的返回類型、參數、方法名稱完全一樣,否則編譯出錯。
- 子類方法不能縮小父類方法的訪問權限。
/**
*
*/
package com.xx.day8;
/**
* @author 程序員小新兒
*2020年4月7日
*/
class Animal {
String name;
int age;
//小動物會叫
public void cry() {
System.out.println("我是一隻小動物,不知道怎麼叫");
}
}
//小貓類
class Cat extends Animal{
//覆蓋父類
public void cry() {
System.out.println("我是一隻小貓,我會喵喵叫");
}
}
//小狗類
class Dog extends Animal{
//覆蓋父類
public void cry() {
System.out.println("我是一隻小狗,我會汪汪叫");
}
}
public class OverRide {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//創建一隻小貓
Cat cat = new Cat();
cat.cry();
//創建一隻小狗
Dog dog = new Dog();
dog.cry();
}
}
- 子類方法不能縮小父類方法的訪問權限。
三、多態
所謂多態,就是指一個引用(類型)在不同情況下的多種狀態。
也可以理解爲:多態是指通過指向父類的指針,來調用在不同子類中實現的方法。
//一般情況下,讓別人訪問私有類型,就生成get、set方法
Java允許父類的引用變量引用它的子類的實例(對象),這種轉換是自動完成的。
Animal animal = new Cat();
代碼示例:
package com.xx.day8;
/**
* @author 程序員小新兒
*2020年4月7日
*/
class Animal{
String name;
int age;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
//小動物會叫
public void cry() {
System.out.println("我是一隻小動物,不知道怎麼叫");
}
//小動物會喫
public void eat() {
System.out.println("我是一隻小動物,不知道愛喫啥");
}
}
//主人類
class Master{
//給動物餵食物.使用多態,方法可以只用一個
public void feed(Animal animal , Food food) {
animal.eat();;
food.showName();
}
}
//食物類
class Food{
String name;
public void showName() {
}
}
//魚類
class Fish extends Food{
//覆蓋父類
public void showName() {
System.out.println("我是一隻小貓,我喫的是魚");
}
}
//骨頭類
class Bone extends Food{
//覆蓋父類
public void showName() {
System.out.println("我是一隻小狗,我喫的是骨頭");
}
}
//小貓類
class Cat extends Animal{
//覆蓋父類
public void cry() {
System.out.println("我是一隻小貓,我會喵喵叫");
}
//貓喫食物
public void eat() {
System.out.println("我是一隻小貓,我愛喫魚");
}
}
//小狗類
class Dog extends Animal{
//覆蓋父類
public void cry() {
System.out.println("我是一隻小狗,我會汪汪叫");
}
//狗喫食物
public void eat() {
System.out.println("我是一隻小狗,我愛喫骨頭");
}
}
public class PolymorphicTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//創建一隻小貓
/*Cat cat = new Cat();
cat.cry();
//創建一隻小狗
Dog dog = new Dog();
dog.cry();*/
//多態
/*Animal animal = new Cat();
animal.cry();
animal = new Dog();
animal.cry();*/
Master master = new Master();
master.feed(new Cat(), new Fish());
master.feed(new Dog(), new Bone());
}
}
輸出結果:
點我進入公衆號原創鏈接
微信掃描下方二維碼或微信公衆號搜索【程序員小新兒】歡迎關注哦~