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());
}
}
输出结果:
点我进入公众号原创链接
微信扫描下方二维码或微信公众号搜索【程序员小新儿】欢迎关注哦~