1.class Test { int a; public int b; private int c; public void setc(int i) { c = i; } public int getc() { return c; } } class AccessTest { public static void main(String args[]) { Test obj = new Test(); obj.a = 10; obj.b = 20; obj.setc(100); System.out.println("a,b,and c: " + obj.a + " " + obj.b + " " + obj.getc()); } } 运行结果:
时间: 2024-04-26 16:25:22 浏览: 19
输出结果为:a,b,and c: 10 20 100
解释:AccessTest 类中创建了 Test 类的实例 obj,并分别给 a、b、c 赋值。因为 a、b 都是 public 类型,所以可以直接访问并赋值。而 c 是 private 类型,不能直接访问,需要通过 setc 和 getc 方法来访问。最后,输出 a、b、c 的值。其中,a 和 b 的值分别为 10 和 20,c 的值为 100,说明 setc 方法设置 c 的值成功。
相关问题
27 6 C:\Users\DELL\Desktop\test2.cpp [Error] 'void B::setB(int)' is inaccessible
### 回答1:
这个错误提示是因为在类C中,类B被声明为私有继承,因此类C的成员函数只能通过类A的公有继承访问类A的成员函数,而不能直接访问类B的成员函数。为了解决这个问题,你可以在类C中定义一个公有的成员函数,来调用类B的成员函数。例如:
```c++
class C : public A, private B {
public:
void setC(int, int, int);
void showC() const;
void setB(int x) { B::setB(x); } // 定义一个公有成员函数来调用类B的成员函数
private:
int c;
};
void C::setC(int x, int y, int z) {
setA(x);
setB(y); // 在类C中调用类B的成员函数
c = z;
}
void C::showC() const {
showA();
B::showB(); // 在类C中调用类B的成员函数
cout << c << endl;
}
```
在这个例子中,我们在类C中定义了一个公有的成员函数`setB()`来调用类B的成员函数`setB()`。这样,我们就可以在类C中调用类B的成员函数了。
### 回答2:
这个错误出现在 C++ 的代码中,错误信息是 'void B::setB(int)' is inaccessible('void B::setB(int)' 不可访问)。
这个错误通常是因为在某个类 B 的代码中调用了其私有或受保护的成员函数 setB(int),而该函数无法被外部访问。
在 C++ 中,类的成员函数默认情况下是私有的。私有成员函数只能在类的内部被调用,外部无法直接访问。受保护成员函数只能在类的内部及其派生类中被访问。
解决这个错误的方法是将 setB(int) 函数的访问权限改为公有(public)。可以在 class B 的声明中将其访问权限修改为 public,如下所示:
```
class B {
public:
void setB(int);
};
void B::setB(int) {
// 函数的实现代码
}
```
这样修改后,外部的代码就可以正常调用 B 类对象的 setB(int) 函数了。
另外,如果该错误是在继承关系中出现的,可能是因为派生类没有正确地访问基类的受保护或私有成员函数。在派生类中,可以使用基类的 public 成员函数来间接访问基类的受保护或私有成员函数。
### 回答3:
根据给出的错误信息,我们可以得知错误发生在文件路径为"C:\Users\DELL\Desktop\test2.cpp",具体是在C++代码的第27行。错误的提示为"'void B::setB(int)' is inaccessible",即访问B类的setB函数是不可访问的。下面是对这个错误的解释和可能的解决方案:
这个错误产生的原因可能是因为setB函数被声明为了私有成员函数或受保护成员函数。私有成员函数只能在类的内部被访问,不能被其他类或对象直接调用。受保护成员函数同样不能被其他类或对象直接调用,但可以被该类的派生类访问。
解决这个问题的方法是将setB函数的访问权限改为public,这样其他类或对象就可以直接调用该函数。在B类中,可能需要将setB函数的访问权限从私有或受保护改为公有。可以通过在B类的定义中将setB函数的访问修饰符改为public来实现,如下所示:
class B {
public:
void setB(int);
};
如果B类是其他类的派生类,那么可以考虑将setB函数的访问权限改为受保护,这样派生类仍然可以访问该函数。修改B类定义如下:
class B {
protected:
void setB(int);
};
最后,根据具体情况选择适当的解决方法,重新编译该源文件以消除错误。
public class TestClock extends JFrame { private String AorP; private String hour; private String minute; private String second; public TestClock() { this.setSize(1000, 800); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(new GridLayout(2, 1)); Panel p1 = new Panel(new GridLayout(1, 3)); while (true) { Calendar c = Calendar.getInstance(); hour = Integer.toString(c.get(Calendar.HOUR)); minute = Integer.toString(c.get(Calendar.MINUTE)); second = Integer.toString(c.get(Calendar.SECOND)); AorP = Integer.toString(c.get(Calendar.AM_PM)); JLabel HH = new JLabel(hour, SwingConstants.CENTER); JLabel MM = new JLabel(minute, SwingConstants.CENTER); JLabel SS = new JLabel(second, SwingConstants.CENTER); JLabel AorP = new JLabel("AM/PM", SwingConstants.CENTER); HH.setFont(new Font(Font.SERIF, Font.ITALIC, 36)); MM.setFont(new Font(Font.SERIF, Font.ITALIC, 36)); SS.setFont(new Font(Font.SERIF, Font.ITALIC, 36)); AorP.setFont(new Font(Font.SERIF, Font.ITALIC, 26)); p1.add(HH); p1.add(MM); p1.add(SS); this.add(p1); this.add(AorP); } } public static void main(String[] args) { // TODO Auto-generated method stub TestClock clock = new TestClock(); } }为什么不显示
这段代码可能会导致程序卡死,因为while(true)会让程序无限循环下去,而且在while循环中不断地创建和添加标签,可能会导致内存溢出。这会导致程序无法正常运行,因此窗口不会显示。
如果想要实现一个实时显示当前时间的时钟,可以使用Timer类来实现。Timer类可以在指定时间间隔内重复执行指定任务,这样就可以实现每隔一段时间更新一次时钟的效果。具体实现可以参考以下代码:
```java
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
public class TestClock extends JFrame {
private JLabel hourLabel;
private JLabel minuteLabel;
private JLabel secondLabel;
private JLabel amPmLabel;
public TestClock() {
this.setSize(300, 150);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(2, 1));
JPanel timePanel = new JPanel(new GridLayout(1, 3));
hourLabel = new JLabel("00", SwingConstants.CENTER);
minuteLabel = new JLabel("00", SwingConstants.CENTER);
secondLabel = new JLabel("00", SwingConstants.CENTER);
timePanel.add(hourLabel);
timePanel.add(minuteLabel);
timePanel.add(secondLabel);
this.add(timePanel);
amPmLabel = new JLabel("AM", SwingConstants.CENTER);
amPmLabel.setFont(new Font(Font.SERIF, Font.ITALIC, 24));
this.add(amPmLabel);
Timer timer = new Timer(1000, e -> {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String amPm = hour < 12 ? "AM" : "PM";
hour = hour % 12;
hourLabel.setText(String.format("%02d", hour));
minuteLabel.setText(String.format("%02d", minute));
secondLabel.setText(String.format("%02d", second));
amPmLabel.setText(amPm);
});
timer.start();
}
public static void main(String[] args) {
TestClock clock = new TestClock();
clock.setVisible(true);
}
}
```
这个时钟每隔1秒更新一次,实时显示当前时间。注意,这里用到了Java 8中的Lambda表达式来简化代码。