public class Run {
目前创新互联已为成百上千家的企业提供了网站建设、域名、雅安服务器托管、网站改版维护、企业网站设计、吉首网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
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("旺财","白色",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("猫会跳高");
}
}
class Gou extends Dongwu implements Suan{
public void suan() {
System.out.println("狗会算数");
}
}
如果有帮助到你,请点击采纳
public class Pet {
//pet的属性
private String name;
private int age;
private char gengder;
//提供无参构造方法
public Pet() {
super();
}
//提供有参构造方法
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 + "]";
}
}
封装的基本语法:
属性私有化
提供无参和有参构造方法
提供get/set方法
重写tostring方法
class 动物{
public boolean 吃(食物 sw){
}
}
class 狗 extends 动物{
public boolean 吃(食物 sw){
if(sw.种类 == 骨头) return true;
else return false;
}
}
class 猫 extends 动物{
public boolean 吃(食物 sw){
if(sw.种类 == 鱼) return true;
else return false;
}
}
你还需要建立一个 食物 类。