開課吧Python全棧工程師培養(yǎng)計(jì)劃
2022-10-13 10:52 作者:janet19961217 | 我要投稿
再創(chuàng)建一個(gè)Dog類,讓Dog類也繼承Pet類,并且也進(jìn)行方法重寫,實(shí)現(xiàn)狗的叫聲
public class Dog extends Pet{
? ?@Override
? ?public void sound(){
? ? ? ?System.out.println("汪汪汪");
? ?}}
在main方法中創(chuàng)建對(duì)象,調(diào)用sound方法,輸出cat和dog的叫聲
public class PetShop{//寵物店
? ?public static void main(String[] args) {
? ? ? ?Dog dog = new Dog();
? ? ? ?dog.sound();//調(diào)用狗的sound()
? ? ? ?Cat cat = new Cat();
? ? ? ?cat.sound();//調(diào)用貓的sound()
? ?}}
標(biāo)簽: