继承、封装和多态 8使用多态

package case8;

public class TestPolymorphism {
        public static void main(String[] args) {
        // 以多态方式分别实例化子类对象并调用eat()方法
        new Dog().eat();
        new Cat().eat();
        new Lion().eat();
        }
}

// Animal类中定义eat()方法
class Animal {
    public void eat(){}
}

// Dog类继承Animal类 复写eat()方法
class Dog extends Animal {
    public void eat(){
        System.out.println("eating bread...");
    }
}

// Cat类继承Animal类 复写eat()方法
class Cat extends Animal {
    public void eat(){
        System.out.println("eating rat...");
    }
}

// Lion类继承Animal类 复写eat()方法
class Lion extends Animal {
    public void eat(){
        System.out.println("eating meat...");
    }
}

FlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET) { timeout++; if (timeout > 1000000) return -1; } data[2] = SPI_I2S_ReceiveData(SPIx); SPI继承封装多态是面向对象程序设计中的三个重要概念。 继承是指一个类x_CS_SET; return ((data[1] << 8) | data[2]) & 0x03FF; } void LCD_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |可以继承另一个类的特性,包括属性和方法,被继承的类称为父类或基类, RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO,继承的类称为子类或派生类。子类可以重写父类的方法,也可以添加自己的 ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed =方法和属性,从而实现代码的复用和扩展。 封装是指将数据和方法封装到一个类 GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); 中,并且对外部隐藏类的实现细节,只暴露必要的接口给外部使用。这样 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5 | GPIO_Pin_6可以提高代码的安全性和可维护性,同时也方便了代码的使用和调用。 多态是 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure指同一种行为或方法在不同的对象上具有不同的表现形式,即同一操作作.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure用于不同的对象上面,可以产生不同的执行结果。多态可以提高代码的灵活性和); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO可扩展性,也是面向对象编程的重要特性之一。 继承封装多态是_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |面向对象编程的基础,它们的实现可以让程序更加高效、简洁、易于维护和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值