想建立一个像素类,继承已经写好的Point类,编译时出现了像素类未定义的错误。

投篮打铁73 2019-10-04 10:21:09
我是Java小白,这个问题是在解决不了了,求各位大佬帮帮我吧!
这是Point类
public class Point{
int x;
int y;

public Point(int x, int y){
this.x = x;
this.y = y;
}

public Point() {
this(0,0);
}

public int getX(){
return x;
}

public void setX(int x){
this.x = x;
}

public int getY(){
return y;
}

public void setY(int y){
this.y = y;
}

}

这是像素Pixel类

public class Pixel extends Point{

private Color color;

public class Color{
int red;
int green;
int blue;

public Color(int red,int green,int blue) {
this.red=red;
this.green=green;
this.blue=blue;
}

public Color() {
this(0,0,0);
}

private String toRGBString() {
return "RGB("+this.red+","+this.green+","+this.blue+")";
}
}
public Pixel(Point p,Color color) {
super(p);
this.color=color;
}

public Pixel() {
this(new Point(0,0),new Color(0,0,0));
}

public String toString(){
return this.getClass().getName()+"像素,坐标"+super.toString()+"颜色"+this.color.toRGBString();
}

public static void main(String args[]) {
System.out.println(new Pixel().toString());
Point p=new Point(100,100);
Color c=new Color(20,15,15);
Pixel pixel=new Pixel(p,c);
System.out.println(pixel.toString());
}
}


谢谢各位大佬了,5555555
...全文
509 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
投篮打铁73 2019-10-04
  • 打赏
  • 举报
回复
引用 11 楼 qybao 的回复:
上面已经说了,非静态内部类实例化需要外部类实利,静态内部类实例化不需要外部类实例
嗷嗷原来是这样,万分谢谢您的耐心解答与指点。
qybao 2019-10-04
  • 打赏
  • 举报
回复
上面已经说了,非静态内部类实例化需要外部类实利,静态内部类实例化不需要外部类实例
投篮打铁73 2019-10-04
  • 打赏
  • 举报
回复
引用 8 楼 qybao 的回复:
你的Color是内部类,要么你就改成静态类 public static class Color(就你的程序来看这是最好的方法), 要么你的Pixel()构造方法就要就不要用new Color(0,0,0),改成null什么的,因为内部类实例化需要外部类的实例,而你这个刚好又是外部类的构造方法,外部类实例没构造好就不能实例化内部类,所以都在没法实例化了
大佬,我还有最后一个问题。就是这里为什么要用静态类呢,关于这一点我不是很明白。
投篮打铁73 2019-10-04
  • 打赏
  • 举报
回复
引用 8 楼 qybao 的回复:
你的Color是内部类,要么你就改成静态类 public static class Color(就你的程序来看这是最好的方法), 要么你的Pixel()构造方法就要就不要用new Color(0,0,0),改成null什么的,因为内部类实例化需要外部类的实例,而你这个刚好又是外部类的构造方法,外部类实例没构造好就不能实例化内部类,所以都在没法实例化了
太感谢您了!!!谢谢大佬
qybao 2019-10-04
  • 打赏
  • 举报
回复
你的Color是内部类,要么你就改成静态类 public static class Color(就你的程序来看这是最好的方法),
要么你的Pixel()构造方法就要就不要用new Color(0,0,0),改成null什么的,因为内部类实例化需要外部类的实例,而你这个刚好又是外部类的构造方法,外部类实例没构造好就不能实例化内部类,所以都在没法实例化了
投篮打铁73 2019-10-04
  • 打赏
  • 举报
回复
引用 2 楼 oh_Maxy 的回复:
如果自己手动编译,要设置一下path环境变量,最前面加上.;
环境变量也弄好了,还是不行,我哭了
投篮打铁73 2019-10-04
  • 打赏
  • 举报
回复
引用 5 楼 qybao 的回复:
public Pixel(Point p,Color color) { super(p); //你的point类没有这样的构造方法,要么用super(p.getX(), p.getY()) this.color=color; }
太强了!!但是现在又提示我No enclosing instance of type Pixel is accessible. Must qualify the allocation with an enclosing instance of type Pixel (e.g. x.new A() where x is an instance of Pixel). 错误在Pixel类的30行和40行
qybao 2019-10-04
  • 打赏
  • 举报
回复
public Pixel(Point p,Color color) {
super(p); //你的point类没有这样的构造方法,要么用super(p.getX(), p.getY())
this.color=color;
}
投篮打铁73 2019-10-04
  • 打赏
  • 举报
回复
引用 2 楼 oh_Maxy的回复:
如果自己手动编译,要设置一下path环境变量,最前面加上.;
怎么设置鸭⊙∀⊙,我太菜了😭
投篮打铁73 2019-10-04
  • 打赏
  • 举报
回复
引用 1 楼 oh_Maxy的回复:
是不是在一个包下? 先编译Point再编译子类试试。
是在同一个包下的,Point类试了试是没有问题的。
oh_Maxy 2019-10-04
  • 打赏
  • 举报
回复
如果自己手动编译,要设置一下path环境变量,最前面加上.;
oh_Maxy 2019-10-04
  • 打赏
  • 举报
回复
是不是在一个包下? 先编译Point再编译子类试试。

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧