public String name;
时间: 2024-05-29 18:10:56 浏览: 4
As an AI language model, I do not have a name. Therefore, the string "name" is simply a variable declaration in Java programming language. It creates an instance variable called "name" that can hold a string value. The variable can be used to store and manipulate the name of an object or entity in the program.
相关问题
public class Address implements Serializable { public String address; public String name; public String phone; }什么意思
这是一个Java类的定义,它实现了Serializable接口,表示该类的对象可以被序列化和反序列化。该类包含三个属性:address、name和phone,它们的类型都是String,表示地址、姓名和电话号码。这三个属性是public访问修饰符,因此可以在该类的外部直接访问和修改这些属性的值。该类可以用于表示一个地址簿中的一个地址条目,它可以通过序列化和反序列化的方式在不同的Java应用程序之间进行传递和共享。
package java111222; public class Animal{ public String name;//名字 protected int age;//年龄 public Animal() {} public Animal(String name,int age) { this.name=name; this.age=age; } public void eat() { System.out.println("animal:"+name+"eating"); } public String getInfo() { return "我是"+name+",今年"+age+"岁了."; } class Cat extends Animal{ public String breed; public Cat() { } public Cat(String name,int age,String breed) { super(name,age); this.breed = breed; } public void eat() { System.out.println(this.breed+super.name+"正在吃鱼"); } public void eat(int num) { System.out.println(this.breed+super.name+"吃了"+num+"条鱼"); } } } public class CatTest { public static void main(String[] args) { Cat cat1 = new Cat("kate",2,"蓝猫"); // TODO 自动生成的方法存根 cat1.eat(); cat1.eat(2); } }
这段代码有几个问题:
1. Animal类和Cat类的定义应该在两个不同的文件中,而不是写在同一个文件中。
2. Animal类中的eat()方法的输出语句中,应该用加号(+)连接字符串和变量,而不是直接用空格连接。
3. Cat类中的eat()方法的输出语句中,应该用加号(+)连接字符串和变量,而不是直接用空格连接。另外,应该在super后面加上".",表示调用父类的属性。
4. CatTest类中的main()方法中,应该在Cat类的定义之前导入Animal类。
修改后的代码如下:
Animal类:
package java111222;
public class Animal {
public String name;//名字
protected int age;//年龄
public Animal() {}
public Animal(String name,int age) {
this.name=name;
this.age=age;
}
public void eat() {
System.out.println("animal:" + name + " eating");
}
public String getInfo() {
return "我是" + name + ",今年" + age + "岁了.";
}
}
Cat类:
package java111222;
public class Cat extends Animal{
public String breed;
public Cat() { }
public Cat(String name,int age,String breed) {
super(name,age);
this.breed = breed;
}
public void eat() {
System.out.println(this.breed + " " + super.name + "正在吃鱼");
}
public void eat(int num) {
System.out.println(this.breed + " " + super.name + "吃了" + num + "条鱼");
}
}
CatTest类:
package java111222;
public class CatTest {
public static void main(String[] args) {
Cat cat1 = new Cat("kate",2,"蓝猫");
cat1.eat();
cat1.eat(2);
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)