java小狗的状态代码 java实验 小狗的状态
Java 狗狗类?
public class Dog {
成都创新互联公司长期为成百上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为汝阳企业提供专业的成都做网站、网站建设、外贸营销网站建设,汝阳网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
/**
* 昵称
*/
private String nickname;
/**
* 品种
*/
private String type;
/**
* 颜色
*/
private String color;
public void selfIntroduction(){
System.out.println("Dog{" +
"昵称='" + nickname + '\'' +
", 品种='" + type + '\'' +
", 颜色='" + color + '\'' +
'}');
}
public Integer speed(){
//不清楚具体需求 可额外设置个属性 返回该属性 或在该方法中写自己的计算公式
return 1;
}
public Dog() {
}
public Dog(String nickname, String type, String color) {
this.nickname = nickname;
this.type = type;
this.color = color;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
java编程急急急
package com.common;
public interface DogState {
public void showState();
}
package com.common;
public class Dog {
private DogState state;
public DogState getState() {
return state;
}
public void setState(DogState state) {
this.state = state;
}
public void show(){
state.showState();
}
}
package com.common.impl;
import com.common.DogState;
public class MeetingDogs implements DogState {
public void showState() {
System.out.println("狗的状态为:上前,嬉闹");
}
}
package com.common.impl;
import com.common.DogState;
public class MeetingEnemy implements DogState {
public void showState() {
System.out.println("狗的状态为:吠叫,很可能咬敌人");
}
}
package com.common.impl;
import com.common.DogState;
public class MeetingFriends implements DogState {
public void showState() {
System.out.println("狗的状态为:摇摇尾巴,表示欢迎");
}
}
package com.common.impl;
import com.common.DogState;
public class MeetingHost implements DogState {
public void showState() {
System.out.println("狗的状态为:开心");
}
}
package com.common;
import com.common.impl.MeetingDogs;
import com.common.impl.MeetingEnemy;
import com.common.impl.MeetingFriends;
import com.common.impl.MeetingHost;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Dog dog = new Dog();
dog.setState(new MeetingDogs());
dog.show();
dog.setState(new MeetingEnemy());
dog.show();
dog.setState(new MeetingFriends());
dog.show();
dog.setState(new MeetingHost());
dog.show();
}
}
java错误: 方法声明无效; 需要返回类型?
看报错信息,可以定位到代码第三行。方法除了构造函数可以没有返回值,其他方法都需要返回值,没有返回值的也需要写void。 如果是构造函数,函数名要跟类名一样
求一个猫狗多态案例的java程序,谢谢
代码如下:
abstract class Animal {
public abstract void eat();
}
class Cat extends Animal {
@Override
public void eat() {
System.out.println("爱吃鱼");
}
public void play() {
System.out.println("玩王者荣耀");
}
}
class Dog extends Animal {
@Override
public void eat() {
System.out.println("爱啃骨头");
}
public void lookDoor() {
System.out.println("看门");
}
}
public class App {
public static void main(String[] argv) {
Cat cat = new Cat();
cat.eat();
cat.play();
Dog dog = new Dog();
dog.eat();
dog.lookDoor();
}
}
分享名称:java小狗的状态代码 java实验 小狗的状态
分享URL:http://scyanting.com/article/docipeo.html