一个关于非静态变量的上下文引用?多谢各位! 这恐怕是你的构造函数没有加上public的原因 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 static方法不可以访问非static的东东改:import java.io.PrintWriter; class PointTest { public class Point2D //¶¨ÒåPoint2DÀà { int x,y; Point2D() //¶¨ÒåPoint2D·½·¨ { this (0,0); } Point2D (int x) { this (x,0); } Point2D (int x,int y) { this.x = x; this.y = y; } double length() { return Math.sqrt(x * x + y * y); //Çóƽ·½¸ùº¯Êý(sqrt) } } public class MyPoint extends Point2D //Éú³É¼Ì³ÐÀàPoint2D { int x,y; MyPoint (int x,int y) { super (x,y); this.x = x; this.y = y; } double length() { return Math.sqrt (x * x + y * y); } double distance() { return Math.abs (length() - super.length()); } } public static void main (String args[]) { new PointTest(); } public PointTest() { PrintWriter out = new PrintWriter (System.out,true); MyPoint mp = new MyPoint(4,3); Point2D p = new Point2D(11); Point2D q = mp; mp.x = 5; mp.y = 12; out.println("\n\tDataMember Access Test:\n"); out.println("mp = (" + mp.x + "," + mp.y + ")"); out.println("p = (" + p.x + "," + p.y + ")"); out.println("q = (" + q.x + "," + q.y + ")"); out.println("\n\tCasttingTest:\n"); out.println("(Point2D)mp = (" + ((Point2D)mp).x + "," + ((Point2D)mp).y + ")"); out.println("(MyPoint)q = (" + ((MyPoint)q).x + "," + ((MyPoint)q).y + ")"); out.println("\n\tMethodMember Access Test:n"); out.println("mp.length() = " + mp.length()); out.println("p.length() = " + p.length()); out.println("q.length() = " + q.length()); out.println("mp.distance() = " + mp.distance()); out.println("\n\tCasting Test:\n"); out.println("((Point2D)mp).length() = " + ((Point2D)mp).length()); out.println("((Point2D)q).length() = " + ((Point2D)q).length()); out.println("((MyPoint)q).distance() = " + ((MyPoint)q).distance()); } } 非静态内部类和其他非静态成员一样,不能直接被main之内的静态成员使用 paste出来出了点问题,重贴import java.io.PrintWriter;class PointTest{ public class Point2D //定义Point2D类 { int x,y; Point2D() //定义Point2D方法 { this (0,0); } Point2D (int x) { this (x,0); } Point2D (int x,int y) { this.x = x; this.y = y; } double length() { return Math.sqrt(x * x + y * y); //求平方根函数(sqrt) } } public class MyPoint extends Point2D //生成继承类Point2D { int x,y; MyPoint (int x,int y) { super (x,y); this.x = x; this.y = y; } double length() { return Math.sqrt (x * x + y * y); } double distance() { return Math.abs (length() - super.length()); } } public static void main (String args[]) { new PointTest(); } public PointTest() { PrintWriter out = new PrintWriter (System.out,true); MyPoint mp = new MyPoint(4,3); Point2D p = new Point2D(11); Point2D q = mp; mp.x = 5; mp.y = 12; out.println("\n\tDataMember Access Test:\n"); out.println("mp = (" + mp.x + "," + mp.y + ")"); out.println("p = (" + p.x + "," + p.y + ")"); out.println("q = (" + q.x + "," + q.y + ")"); out.println("\n\tCasttingTest:\n"); out.println("(Point2D)mp = (" + ((Point2D)mp).x + "," + ((Point2D)mp).y + ")"); out.println("(MyPoint)q = (" + ((MyPoint)q).x + "," + ((MyPoint)q).y + ")"); out.println("\n\tMethodMember Access Test:n"); out.println("mp.length() = " + mp.length()); out.println("p.length() = " + p.length()); out.println("q.length() = " + q.length()); out.println("mp.distance() = " + mp.distance()); out.println("\n\tCasting Test:\n"); out.println("((Point2D)mp).length() = " + ((Point2D)mp).length()); out.println("((Point2D)q).length() = " + ((Point2D)q).length()); out.println("((MyPoint)q).distance() = " + ((MyPoint)q).distance()); }} 用new PointTest().new MyPoint(4,3)的形式构造实例或把内部类改成static 这个是新的问题:Exception in thread "main" java.lang.NoClassDefFoundError: PointClass1/java java PointClass1不要加.java 源程序import java.io.PrintWriter; public class Point2D //定义Point2D类 { int x,y; Point2D() //定义Point2D方法 { this (0,0); } Point2D (int x) { this (x,0); } Point2D (int x,int y) { this.x = x; this.y = y; } double length() { return Math.sqrt(x * x + y * y); //求平方根函数(sqrt) } } public class MyPoint extends Point2D //生成继承类Point2D { int x,y; MyPoint (int x,int y) { super (x,y); this.x = x; this.y = y; } double length() { return Math.sqrt (x * x + y * y); } double distance() { return Math.abs (length() - super.length()); } } public class PointTest { public static void main (String args[]) { PrintWriter out = new PrintWriter (System.out,true); MyPoint mp = new MyPoint(4,3); Point2D p = new Point2D(11); Point2D q = mp; mp.x = 5; mp.y = 12; out.println("\n\tDataMember Access Test:\n"); out.println("mp = (" + mp.x + "," + mp.y + ")"); out.println("p = (" + p.x + "," + p.y + ")"); out.println("q = (" + q.x + "," + q.y + ")"); out.println("\n\tCasttingTest:\n"); out.println("(Point2D)mp = (" + ((Point2D)mp).x + "," + ((Point2D)mp).y + ")"); out.println("(MyPoint)q = (" + ((MyPoint)q).x + "," + ((MyPoint)q).y + ")"); out.println("\n\tMethodMember Access Test:n"); out.println("mp.length() = " + mp.length()); out.println("p.length() = " + p.length()); out.println("q.length() = " + q.length()); out.println("mp.distance() = " + mp.distance()); out.println("\n\tCasting Test:\n"); out.println("((Point2D)mp).length() = " + ((Point2D)mp).length()); out.println("((Point2D)q).length() = " + ((Point2D)q).length()); out.println("((MyPoint)q).distance() = " + ((MyPoint)q).distance()); } }执行的结果PointClass.java:2: 类 Point2D 是 公共的(public〕,应该在名为 Point2D.java 的文件中被声明 public class Point2D //定义Point2D类 ^PointClass.java:23: 类 MyPoint 是 公共的(public〕,应该在名为 MyPoint.java 的文件中被声明 public class MyPoint extends Point2D //生成继承类Point2D ^PointClass.java:38: 类 PointTest 是 公共的(public〕,应该在名为 PointTest.java的文件中被声明 public class PointTest ^3 个错误我已声明的了??? 在第4行定义Point2D类语句中加入static即:public static class Point2D在第25行定义生成继承类Point2D语句中加入static即:public static class MyPoint extends Point2D 求解,求解数据库SQL问题 数据库乱码解决 请问这个程序会有错误,请大家看一下,这到底错在哪里啊,急... import 怎么用? 请大家进来看看,有惊喜哦! 高分求解几个问题 jsp文件中包含一个applet,如何把applet的运行结果传给jsp文件使用? 什么地方可以下载JBuilder8完全正式企业版? 那位老大查询过EXCEL文件,请进来帮忙!多谢 求大佬指点。。Swing 在 c:\jdk\1.3.1_02文件夹下的 src.jar 文件起什么作用? 这个错误和解(100分)
改:import java.io.PrintWriter;
class PointTest
{
public class Point2D //¶¨ÒåPoint2DÀà
{
int x,y;
Point2D() //¶¨ÒåPoint2D·½·¨
{
this (0,0);
}
Point2D (int x)
{
this (x,0);
}
Point2D (int x,int y)
{
this.x = x;
this.y = y;
}
double length()
{
return Math.sqrt(x * x + y * y); //Çóƽ·½¸ùº¯Êý(sqrt)
}
}
public class MyPoint extends Point2D //Éú³É¼Ì³ÐÀàPoint2D
{
int x,y;
MyPoint (int x,int y) {
super (x,y);
this.x = x;
this.y = y;
}
double length() {
return Math.sqrt (x * x + y * y);
}
double distance() {
return Math.abs (length() - super.length());
}
}
public static void main (String args[])
{
new PointTest();
} public PointTest() {
PrintWriter out = new PrintWriter (System.out,true);
MyPoint mp = new MyPoint(4,3);
Point2D p = new Point2D(11);
Point2D q = mp;
mp.x = 5;
mp.y = 12;
out.println("\n\tDataMember Access Test:\n");
out.println("mp = (" + mp.x + "," + mp.y + ")");
out.println("p = (" + p.x + "," + p.y + ")");
out.println("q = (" + q.x + "," + q.y + ")");
out.println("\n\tCasttingTest:\n");
out.println("(Point2D)mp = (" + ((Point2D)mp).x + "," + ((Point2D)mp).y + ")");
out.println("(MyPoint)q = (" + ((MyPoint)q).x + "," + ((MyPoint)q).y + ")");
out.println("\n\tMethodMember Access Test:n");
out.println("mp.length() = " + mp.length());
out.println("p.length() = " + p.length());
out.println("q.length() = " + q.length());
out.println("mp.distance() = " + mp.distance());
out.println("\n\tCasting Test:\n");
out.println("((Point2D)mp).length() = " + ((Point2D)mp).length());
out.println("((Point2D)q).length() = " + ((Point2D)q).length());
out.println("((MyPoint)q).distance() = " + ((MyPoint)q).distance());
}
}
class PointTest
{
public class Point2D //定义Point2D类
{
int x,y;
Point2D() //定义Point2D方法
{
this (0,0);
}
Point2D (int x)
{
this (x,0);
}
Point2D (int x,int y)
{
this.x = x;
this.y = y;
}
double length()
{
return Math.sqrt(x * x + y * y); //求平方根函数(sqrt)
}
}
public class MyPoint extends Point2D //生成继承类Point2D
{
int x,y;
MyPoint (int x,int y) {
super (x,y);
this.x = x;
this.y = y;
}
double length() {
return Math.sqrt (x * x + y * y);
}
double distance() {
return Math.abs (length() - super.length());
}
}
public static void main (String args[])
{
new PointTest();
} public PointTest() {
PrintWriter out = new PrintWriter (System.out,true);
MyPoint mp = new MyPoint(4,3);
Point2D p = new Point2D(11);
Point2D q = mp;
mp.x = 5;
mp.y = 12;
out.println("\n\tDataMember Access Test:\n");
out.println("mp = (" + mp.x + "," + mp.y + ")");
out.println("p = (" + p.x + "," + p.y + ")");
out.println("q = (" + q.x + "," + q.y + ")");
out.println("\n\tCasttingTest:\n");
out.println("(Point2D)mp = (" + ((Point2D)mp).x + "," + ((Point2D)mp).y + ")");
out.println("(MyPoint)q = (" + ((MyPoint)q).x + "," + ((MyPoint)q).y + ")");
out.println("\n\tMethodMember Access Test:n");
out.println("mp.length() = " + mp.length());
out.println("p.length() = " + p.length());
out.println("q.length() = " + q.length());
out.println("mp.distance() = " + mp.distance());
out.println("\n\tCasting Test:\n");
out.println("((Point2D)mp).length() = " + ((Point2D)mp).length());
out.println("((Point2D)q).length() = " + ((Point2D)q).length());
out.println("((MyPoint)q).distance() = " + ((MyPoint)q).distance());
}
}
或把内部类改成static
Exception in thread "main" java.lang.NoClassDefFoundError: PointClass1/java
不要加.java
import java.io.PrintWriter;
public class Point2D //定义Point2D类
{
int x,y;
Point2D() //定义Point2D方法
{
this (0,0);
}
Point2D (int x)
{
this (x,0);
}
Point2D (int x,int y)
{
this.x = x;
this.y = y;
}
double length()
{
return Math.sqrt(x * x + y * y); //求平方根函数(sqrt)
}
}
public class MyPoint extends Point2D //生成继承类Point2D
{
int x,y;
MyPoint (int x,int y) {
super (x,y);
this.x = x;
this.y = y;
}
double length() {
return Math.sqrt (x * x + y * y);
}
double distance() {
return Math.abs (length() - super.length());
}
}
public class PointTest
{
public static void main (String args[])
{
PrintWriter out = new PrintWriter (System.out,true);
MyPoint mp = new MyPoint(4,3);
Point2D p = new Point2D(11);
Point2D q = mp;
mp.x = 5;
mp.y = 12;
out.println("\n\tDataMember Access Test:\n");
out.println("mp = (" + mp.x + "," + mp.y + ")");
out.println("p = (" + p.x + "," + p.y + ")");
out.println("q = (" + q.x + "," + q.y + ")");
out.println("\n\tCasttingTest:\n");
out.println("(Point2D)mp = (" + ((Point2D)mp).x + "," + ((Point2D)mp).y + ")");
out.println("(MyPoint)q = (" + ((MyPoint)q).x + "," + ((MyPoint)q).y + ")");
out.println("\n\tMethodMember Access Test:n");
out.println("mp.length() = " + mp.length());
out.println("p.length() = " + p.length());
out.println("q.length() = " + q.length());
out.println("mp.distance() = " + mp.distance());
out.println("\n\tCasting Test:\n");
out.println("((Point2D)mp).length() = " + ((Point2D)mp).length());
out.println("((Point2D)q).length() = " + ((Point2D)q).length());
out.println("((MyPoint)q).distance() = " + ((MyPoint)q).distance());
}
}
执行的结果
PointClass.java:2: 类 Point2D 是 公共的(public〕,应该在名为 Point2D.java 的文
件中被声明
public class Point2D //定义Point2D类
^
PointClass.java:23: 类 MyPoint 是 公共的(public〕,应该在名为 MyPoint.java 的文
件中被声明
public class MyPoint extends Point2D //生成继承类Poin
t2D
^
PointClass.java:38: 类 PointTest 是 公共的(public〕,应该在名为 PointTest.java
的文件中被声明
public class PointTest
^
3 个错误
我已声明的了???
即:public static class Point2D
在第25行定义生成继承类Point2D语句中加入static
即:public static class MyPoint extends Point2D