求java代码!!谢谢

2025-01-02 23:17:21
推荐回答(4个)
回答1:

//包路径你自己换
package pc.test;

public class Dog {
private String name;
private int age;
private int weight;

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 int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

public String say() {
return "Dog [name=" + name + ", age=" + age + ", weight=" + weight + "]";
}

}
//包路径自己换
package pc.test;

public class TestDog {

public static void main(String[] args) {
Dog dog = new Dog();
dog.setName("小狗");
dog.setAge(1);
dog.setWeight(1);
System.out.println(dog.say());
}

}

回答2:

package 你自己的报名,或者删掉;
/*
*@author admin
*@date 2017年9月28日
*@time 下午5:39:35
*TODO 你得自己理解一下,基本满足了你的需求,可以只建一个类,也可换换写成两
*个类
*/
public class TestDog{
public static void main(String[] args) {
Dog dog = new Dog("小黄",18,"30kg");
String msg = dog.say();
System.out.println(msg);
}
}

class Dog {
private String name;
private int age;
private String weight;

public Dog(String name, int age, String weight) {
this.name = name;
this.age = age;
this.weight = weight;
}

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 String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String say() {
return "introduce myself [name=" + name + ", age=" + age + ", weight=" + weight + "]";
}

}

回答3:

package ceshi;

public class Dog {
String name;
int age;
Double weight;

public Dog(String name, int age, Double weight) {
super();
this.name = name;
this.age = age;
this.weight = weight;
}
public Dog() {

}
public String say(){
return name+","+age+","+weight;
}
}
//===========================================
package ceshi;

public class DogTest {

public static void main(String[] args) {
Dog dog=new Dog("二哈",3,10.2);
String str=dog.say();
System.out.println(str);

}

}
控制台输出:
二哈,3,10.2

回答4:

作业还是要自己写,毕竟也是太基础的东西了。