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 15:25:22 浏览: 59
输出结果为: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 的值成功。
相关问题
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表达式来简化代码。
package main.mymath; import java.util.InputMismatchException; import java.util.Scanner; public class MyCalculator { //Addition public void add(int num1,int1 num2) { int result; result = num1 + num2; if(result > Integer.MAX_VALUE || result < Integer.MIN_VALUE){ throw new IllegalArgumentException("输入数字导致溢出"); }else { System.out.println(result); } } //Multiplication public void subtraction(double num1,double num2) { System.out.println(num1 - num2); } //Division public void divide(double num1,double num2) { if(num2==0) { throw new IllegalArgumentException("分母不能为零"); }else { System.out.println(num1 / num2); } } public void calc() { Scanner sc = new Scanner(System.in); System.out.println("premier nombre:"); int num1 = sc.nextInt(); System.out.println("deuxieme nombre:"); int num2 = sc.nextInt(); System.out.println("choix【+,-,/】"); char op = sc.next().charAt(0); switch (op) { case '+': add(num1, num2); break; case '-': subtraction(num1, num2); break; case '/': divide(num1, num2); break; default: System.out.println("erreur!"); break; } } public static void main(String[] args) { MyCalculator c = new MyCalculator(); c.calc(); } }
好的,根据你提供的代码,以下是一个可能的测试类示例:
```
package test.mymath;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import java.io.ByteArrayInputStream;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import org.junit.Before;
import org.junit.Test;
import main.mymath.MyCalculator;
public class MyCalculatorTest {
private MyCalculator calculator;
@Before
public void setUp() {
calculator = new MyCalculator();
}
@Test
public void testAdd() {
assertEquals(5, calculator.add(2, 3));
assertEquals(-1, calculator.add(2, -3));
assertEquals(0, calculator.add(0, 0));
assertThrows(IllegalArgumentException.class, () -> calculator.add(Integer.MAX_VALUE, 1));
}
@Test
public void testSubtraction() {
assertEquals(2.0, calculator.subtraction(5.0, 3.0), 0.0);
assertEquals(-2.0, calculator.subtraction(-5.0, -3.0), 0.0);
assertEquals(0.0, calculator.subtraction(0.0, 0.0), 0.0);
assertEquals(0.1, calculator.subtraction(0.3, 0.2), 0.0);
}
@Test
public void testDivide() {
assertEquals(2.0, calculator.divide(6.0, 3.0), 0.0);
assertEquals(-2.0, calculator.divide(6.0, -3.0), 0.0);
assertEquals(0.0, calculator.divide(0.0, 1.0), 0.0);
assertThrows(IllegalArgumentException.class, () -> calculator.divide(6.0, 0.0));
}
@Test
public void testCalc() {
String input = "2\n3\n+\n";
System.setIn(new ByteArrayInputStream(input.getBytes()));
Scanner scanner = new Scanner(System.in);
calculator.calc(scanner);
assertEquals("5\n", systemOut().getHistory());
input = "5.0\n3.0\n-\n";
System.setIn(new ByteArrayInputStream(input.getBytes()));
scanner = new Scanner(System.in);
calculator.calc(scanner);
assertEquals("2.0\n", systemOut().getHistory());
input = "4.0\n2.0\n/\n";
System.setIn(new ByteArrayInputStream(input.getBytes()));
scanner = new Scanner(System.in);
calculator.calc(scanner);
assertEquals("2.0\n", systemOut().getHistory());
}
@Test
public void testCalcInvalidInput() {
String input = "invalid\ninput\n";
System.setIn(new ByteArrayInputStream(input.getBytes()));
Scanner scanner = new Scanner(System.in);
assertThrows(NoSuchElementException.class, () -> calculator.calc(scanner));
}
private SystemOut systemOut() {
return new SystemOut();
}
private static class SystemOut extends java.io.ByteArrayOutputStream {
public String getHistory() {
return new String(this.toByteArray());
}
}
}
```
在上面的测试类示例中,我们使用JUnit框架来测试MyCalculator类的三个方法:add(),subtraction()和divide(),并使用标准输入输出模拟用户交互,测试calc()方法是否能够正确的计算加、减、除三种运算。同时,我们还测试了一些输入无效数据的情况,来保证程序的健壮性。
阅读全文