永发信息网

在main方法中创建一个新的Point对象,并把其分配给变量p

答案:2  悬赏:0  手机版
解决时间 2021-03-23 10:10
  • 提问者网友:
  • 2021-03-23 07:20
在main方法中创建一个新的Point对象,并把其分配给变量p
最佳答案
  • 五星知识达人网友:罪歌
  • 2021-03-23 08:56
因为Point类是Rectangle类的内部类,所以需要用内部类的创建方法新建Point类的对象p
Rectangle.Point p=rect.new Point();
完整的Java程序如下
import javax.security.auth.x500.*;
public class Main
{
 public static void main(String[] args)
 {
  Rectangle rect = new Rectangle();
  rect.width = 100;
  System.out.println(rect.width);
  rect.height = 200;
  System.out.println(rect.height);
  Rectangle rect2 = new Rectangle();
  rect2.width = 10;
  System.out.println(rect2.width);
  rect2.height = 20;
  System.out.println(rect2.height);
  Rectangle.Point p=rect.new Point();
  p.x=30;
  p.y=40;
  System.out.println(p.x);
  System.out.println(p.y);
 }
}
class Rectangle
{
 int width;
 int height;
 class Point
 {
  int x;
  int y;
 }
}运行结果
100
200
10
20
30
40

全部回答
  • 1楼网友:枭雄戏美人
  • 2021-03-23 09:41
因为Point类是Rectangle类的内部类,所以需要用内部类的创建方法新建Point类的对象p
Rectangle.Point p=rect.new Point();
完整的Java程序如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

import javax.security.auth.x500.*;
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
rect.width = 100;
System.out.println(rect.width);
rect.height = 200;
System.out.println(rect.height);
Rectangle rect2 = new Rectangle();
rect2.width = 10;
System.out.println(rect2.width);
rect2.height = 20;
System.out.println(rect2.height);
Rectangle.Point p=rect.new Point();
p.x=30;
p.y=40;
System.out.println(p.x);
System.out.println(p.y);
}
}
class Rectangle
{
int width;
int height;
class Point
{
int x;
int y;
}
}

运行结果
10020010203040
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯