1 class Tom1 { 2 Tom1(int m) { 3 System.out.print(m); 4 } 5 } 6 public class E3 { 7 public static void main (String args[]){ 8 Tom1 a=new Tom1() ; 9 } 10 }
时间: 2024-03-25 08:36:04 浏览: 28
这段代码会报错,因为在第8行中创建Tom1对象时没有传入必要的int参数m,而Tom1类的构造方法中需要一个int参数m。因此,编译器会提示“构造方法Tom1(int)未定义”的错误。
要修复这个错误,需要在创建Tom1对象时传入一个int类型的参数,例如:
```
Tom1 a = new Tom1(10);
```
在这个例子中,传入的参数为10,将被输出到控制台上。如果想输出其他的数字,只需要改变传入的参数即可。
相关问题
java上机实践_[转载]JAVA上机实践: 继承与接口-继承
好的,关于Java上机实践的问题,我可以帮您解答。这里是继承的相关内容:
Java中的继承是面向对象编程中的一项重要特性,它允许子类继承超类的所有属性和方法。子类可以在不重复编写代码的情况下拥有超类的所有特性,并且可以在超类的基础上添加自己的功能。
下面是一个简单的例子:
```
class Animal {
String name;
int age;
public void eat() {
System.out.println("Animal is eating...");
}
}
class Cat extends Animal {
public void meow() {
System.out.println("Cat is meowing...");
}
}
public class Test {
public static void main(String[] args) {
Cat cat = new Cat();
cat.name = "Tom";
cat.age = 3;
cat.eat(); // 继承自Animal类
cat.meow(); // 自己的方法
}
}
```
在这个例子中,`Cat`类继承了`Animal`类,因此它也有`name`和`age`属性,以及`eat()`方法。此外,它还有自己的方法`meow()`。
当我们实例化一个`Cat`对象时,我们可以使用`name`和`age`属性,以及`eat()`和`meow()`方法。
继承是Java中的一项强大的特性,它可以使代码更具可读性和可维护性,并减少代码的重复编写。
class+Tom+{ +private+int+x+=+120; +protected+int+y+=+20; +int+z+=+11; +private+void
Tom类中有三个成员变量x、y、z,其中x是私有变量,y是受保护的变量,z是默认访问修饰符的变量。Tom类中有两个方法f和g,其中f是私有方法,g是默认访问修饰符的方法。私有变量和方法只能在类内部被访问,不能在类外部被访问。受保护的变量和默认访问修饰符的变量可以在同一个包中或者是子类中被访问。\n\
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)