public?class?Run?{
目前創(chuàng)新互聯(lián)已為成百上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、吉首網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
public?static?void?main(String[]?args)?{
Master?master?=?new?Master();
master.feedDog("雞骨頭");
master.feedCat("雞骨頭");
}
}
class?Master?{
private?Pet?mPet;
private?Food?mFood;
public?void?feedCat(String?food)?{
mPet?=?new?Cat();
mFood?=?new?Food(food);
mPet.eat(mFood);
}
public?void?feedDog(String?food)?{
mPet?=?new?Dog();
mFood?=?new?Food(food);
mPet.eat(mFood);
}
}
class?Dog?extends?Pet{
@Override
public?void?eat(Food?food)?{
System.out.println("正在喂小狗吃"+food.getFood());
if?(food.getFood().matches(Food.BONE))?{
System.out.println("小狗正在吃"+food.getFood()+"!");
}else?{
System.out.println("但是小狗不喜歡吃"+food.getFood()+"!");
}
}
}
class?Cat?extends?Pet{
@Override
public?void?eat(Food?food)?{
System.out.println("正在喂小貓吃"+food.getFood());
if?(food.getFood().matches(Food.FISH))?{
System.out.println("小貓正在吃"+food.getFood()+"!");
}else?{
System.out.println("但是小貓不喜歡吃"+food.getFood()+"!");
}
}
}
class?Food?{
public?final?static?String?BONE?=?".*骨.*";
public?final?static?String?FISH?=?".*魚.*";
private?String?food;
public?String?getFood()?{
return?food;
}
public?void?setFood(String?food)?{
this.food?=?food;
}
public?Food(String?food)?{
this.food?=?food;
}
}
class?Pet?{
public?void?eat(Food?food)?{
}
}
public class Dog {
String dogName;
int dogAge;
public Dog()
{
dogName="大黃";
dogAge=5;
}
public void dogWalk()
{
System.out.println("挖得意的笑!挖得意的笑!");
}
}
public class Smalldog extends Dog{
}
public class Test {
public static void main(String[] args) {
Smalldog sd=new Smalldog();
System.out.println(sd.dogName);
System.out.println(sd.dogAge);
sd.dogWalk();
}
}
class Dog{
private String name;
private String color;
private int age;
public Dog(String name,String color,int age){
this.name=name;
this.color=color;
this.age=age;
}
public void show(){
System.out.println("狗的名字:"+name);
System.out.println("狗的顏色:"+color);
System.out.println("狗的年齡:"+age);
}
}
class DogText{
public static void main(String args[]){
Dog dog1=new Dog("來福","黑色",7);
Dog dog2=new Dog("旺財(cái)","白色",3);
dog1.show();
dog2.show();
}
}
代碼如下
abstract class Dongwu {
void chi(){
System.out.println("吃");
}
}
interface Tiao{
void tiao();
}
interface Suan{
void suan();
}
class Mao extends Dongwu implements Tiao{
public void tiao() {
System.out.println("貓會(huì)跳高");
}
}
class Gou extends Dongwu implements Suan{
public void suan() {
System.out.println("狗會(huì)算數(shù)");
}
}
如果有幫助到你,請(qǐng)點(diǎn)擊采納
public?class?Pet?{
//pet的屬性
private?String?name;
private?int?age;
private?char?gengder;
//提供無參構(gòu)造方法
public?Pet()?{
super();
}
//提供有參構(gòu)造方法
public?Pet(String?name,?int?age,?char?gengder)?{
super();
this.name?=?name;
this.age?=?age;
this.gengder?=?gengder;
}
//提供get/set方法
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?int?getAge()?{
return?age;
}
public?void?setAge(int?age)?{
this.age?=?age;
}
public?char?getGengder()?{
return?gengder;
}
public?void?setGengder(char?gengder)?{
this.gengder?=?gengder;
}
//重寫tostring方法
@Override
public?String?toString()?{
return?"Pet?[name="?+?name?+?",?age="?+?age?+?",?gengder="?+?gengder?+?"]";
}
}
封裝的基本語法:
屬性私有化
提供無參和有參構(gòu)造方法
提供get/set方法
重寫tostring方法
class 動(dòng)物{
public boolean 吃(食物 sw){
}
}
class 狗 extends 動(dòng)物{
public boolean 吃(食物 sw){
if(sw.種類 == 骨頭) return true;
else return false;
}
}
class 貓 extends 動(dòng)物{
public boolean 吃(食物 sw){
if(sw.種類 == 魚) return true;
else return false;
}
}
你還需要建立一個(gè) 食物 類。