class Class1 { private int y; private void show() { system.out.println("show method is private"); } public void usesecret() { show(); } } public class ClassDemo { public static void main(String args[]) { ClassDemo1 Ob = new ClassDemo1(); System.out.println(Ob.y); Ob.show(); Ob.usesecret(); } } 指出错误的原因。
时间: 2024-02-24 17:56:35 浏览: 23
这段代码存在以下错误:
1. 类名不一致。类 `Class1` 和类 `ClassDemo1` 的名称不同。
2. `y` 和 `show()` 方法都是私有的,不能从类的外部访问。在 `ClassDemo` 类的 `main` 方法中,不能直接访问 `Ob.y` 和 `Ob.show()`。
3. 在 `ClassDemo` 类的 `main` 方法中,应该使用 `Class1` 类的对象来调用 `usesecret()` 方法,而不是 `ClassDemo1`。
正确的代码如下:
```
class Class1 {
private int y;
private void show() {
System.out.println("show method is private");
}
public void usesecret() {
show();
}
}
public class ClassDemo {
public static void main(String args[]) {
Class1 Ob = new Class1();
System.out.println(Ob.y); //错误,不能访问私有变量
//Ob.show(); //错误,不能访问私有方法
Ob.usesecret();
}
}
```
相关问题
检查以下代码是否有逻辑错误,应该如何修改package money; import java.util.Scanner; public class Money { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int fen = in.nextInt(); Moneys x = new Moneys(fen); x.conversion(); x.show(); in.close(); } } class Moneys{ private int fen; private int jiao; private int yuan; public void conversion(){ this.fen = fen%10; this.jiao = fen/10%10; this.yuan = fen/100; } public void show() { System.out.println(yuan+"yuan"+jiao+"jiao"+fen+"fen"); } public Moneys(int fen) { this.fen = fen; } public Moneys(int yuan,int jiao,int fen) { this.fen = fen; this.jiao = jiao; this.yuan = yuan; } }
代码存在逻辑错误,因为在Moneys类的conversion方法中,fen没有被初始化,因此无输入多少钱数,都会0元0角0分应该修改为以下:
```
class Moneys{
private int;
private int jiao;
private int;
public void conversion(){
this.fen this.fen%10;
this.jiao = this.fen/10%;
this.yuan =.fen/100;
public void show() {
System.out.println(yuan+"yuan"+jiao+"jiao"+fen+"fen");
}
public Moneys(int fen) {
this.fen = fen;
}
public Moneys(int yuan,int jiao,int fen) {
this.fen = fen;
this.jiao = jiao;
this.yuan = yuan;
}
}
```
在conversion方法中,应该使用this.fen来指代类的属性fen,而不是局部变量fen。
编写一个员工类employee,包含name\sex\deptid\salary,分别为姓名、性别、部门编号、薪水。其中薪水为私有属性(即需要设计对应的get\set方法),包含一个方法为show()用于显示属性值。包含二个构造方法:一个不带参数,直接输出“this is in employee gouzao method.”。第二个带二个参数,为name和salary进行赋值。
以下是代码实现:
```java
class Employee {
private String name;
private String sex;
private int deptid;
private double salary;
public Employee() {
System.out.println("this is in employee gouzao method.");
}
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getDeptid() {
return deptid;
}
public void setDeptid(int deptid) {
this.deptid = deptid;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void show() {
System.out.println("name=" + name + ", sex=" + sex + ", deptid=" + deptid + ", salary=" + salary);
}
}
```
这是员工类的代码,包含了题目要求的name、sex、deptid、salary属性,以及get、set方法和一个show方法用于显示属性值,还有两个构造方法,一个是不带参数的,另一个是带name、salary参数的。
如果需要测试这个类,可以在main方法中创建一个Employee对象,并使用set方法设置属性值,最后调用show方法显示属性值。