问题紧急

xixigongzhu 2003-06-18 08:48:21
在java的class里有个静态的(static)内部class,编译通过,但是在sample里面用的时候,却出现的编译错,后来查出这个内部class的构造函数比实际写的多出了一个参数,为什么会这样呢?

条件同上,也是个静态的内部class,但是在sample里面用的时候,如果用mainclass.nestedclass来访问构造函数时报"not an enclosing class"的错,明明是个静态的怎么不能这样用?

以上两个问题class的代码都超过了2000行。
...全文
77 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yoken 2003-06-19
  • 打赏
  • 举报
回复
Default constructors
As mentioned previously, a default constructor (a.k.a. a “no-arg” constructor) is one without arguments that is used to create a “basic object.” If you create a class that has no constructors, the compiler will automatically create a default constructor for you. For example: Feedback


//: c04:DefaultConstructor.java

class Bird {
int i;
}

public class DefaultConstructor {
public static void main(String[] args) {
Bird nc = new Bird(); // Default!
}
} ///:~



The line Feedback


new Bird();



creates a new object and calls the default constructor, even though one was not explicitly defined. Without it, we would have no method to call to build our object. However, if you define any constructors (with or without arguments), the compiler will not synthesize one for you: Feedback


class Hat {
Hat(int i) {}
Hat(double d) {}
}



Now if you say: Feedback


new Hat();



the compiler will complain that it cannot find a constructor that matches. It’s as if when you don’t put in any constructors, the compiler says “You are bound to need some constructor, so let me make one for you.” But if you write a constructor, the compiler says “You’ve written a constructor so you know what you’re doing; if you didn’t put in a default it’s because you meant to leave it out.” Feedback

yoken 2003-06-19
  • 打赏
  • 举报
回复
Nested classes

If you don’t need a connection between the inner class object and the outer class object, then you can make the inner class static. This is commonly called a nested class.[36] To understand the meaning of static when applied to inner classes, you must remember that the object of an ordinary inner class implicitly keeps a reference to the object of the enclosing class that created it. This is not true, however, when you say an inner class is static. A nested class means: Feedback


You don’t need an outer-class object in order to create an object of a nested class. Feedback
You can’t access a non-static outer-class object from an object of a nested class. Feedback
Nested classes are different from ordinary inner classes in another way, as well. Fields and methods in ordinary inner classes can only be at the outer level of a class, so ordinary inner classes cannot have static data, static fields, or nested classes. However, nested classes can have all of these: Feedback


//: c08:Parcel10.java
// Nested classes (static inner classes).

public class Parcel10 {
private static class ParcelContents implements Contents {
private int i = 11;
public int value() { return i; }
}
protected static class ParcelDestination
implements Destination {
private String label;
private ParcelDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
// Nested classes can contain other static elements:
public static void f() {}
static int x = 10;
static class AnotherLevel {
public static void f() {}
static int x = 10;
}
}
public static Destination dest(String s) {
return new ParcelDestination(s);
}
public static Contents cont() {
return new ParcelContents();
}
public static void main(String[] args) {
Contents c = cont();
Destination d = dest("Tanzania");
}
} ///:~



In main( ), no object of Parcel10 is necessary; instead, you use the normal syntax for selecting a static member to call the methods that return references to Contents and Destination. Feedback


As you will see shortly, in an ordinary (non-static) inner class, the link to the outer class object is achieved with a special this reference. A nested class does not have this special this reference, which makes it analogous to a static method. Feedback


Normally, you can’t put any code inside an interface, but a nested class can be part of an interface. Since the class is static, it doesn’t violate the rules for interfaces—the nested class is only placed inside the namespace of the interface:


//: c08:IInterface.java
// Nested classes inside interfaces.

public interface IInterface {
static class Inner {
int i, j, k;
public Inner() {}
void f() {}
}
} ///:~



Earlier in this book I suggested putting a main( ) in every class to act as a test bed for that class. One drawback to this is the amount of extra compiled code you must carry around. If this is a problem, you can use a nested class to hold your test code: Feedback


//: c08:TestBed.java
// Putting test code in a nested class.

public class TestBed {
public TestBed() {}
public void f() { System.out.println("f()"); }
public static class Tester {
public static void main(String[] args) {
TestBed t = new TestBed();
t.f();
}
}
} ///:~



This generates a separate class called TestBed$Tester (to run the program, you say java TestBed$Tester). You can use this class for testing, but you don’t need to include it in your shipping product; you can simply delete TestBed$Tester.class before packaging things up. Feedback

pzl686 2003-06-19
  • 打赏
  • 举报
回复
不会报错呀
你把类名第一个字母改成大写试试
xixigongzhu 2003-06-19
  • 打赏
  • 举报
回复
类似这样:
//mainclass.java
public class mainclass {
pulbic mainclass(String str) {
}
public static nestedclass getNested() {
return new nestedclass();
}
public static class nestedclass{
public nestedclass(){
}
}
}

//sample.java
public class test{
public void testmethod(){
mainclass mc = new mainclass("aa");//报错,说没有这个构造函数;
mainclass.nestedclass mn = new mainclass.nestedclass();//报not an enclosing class的错,
mn = mainclass.getNested();//这个不会报错
}
}

我在怀疑是不是代码太长了,在用的时候丢失了某些东西。
zhjjava 2003-06-19
  • 打赏
  • 举报
回复
没听说过静态类,只听说过静态方法!
如果在class a{
public static void call(){
System.out.println("hi");
}
}
则可以直接a.call();
你如果调用失败,请注意你的修饰符!
public static void call(){//public?
还有类a的修饰!
好象我所知道的只能是public或默认(无)
Hodex 2003-06-18
  • 打赏
  • 举报
回复
用mainclass.nestedclass来访问构造函数
什么意思
错误代码说那不是嵌套类
loveforrr 2003-06-18
  • 打赏
  • 举报
回复
同意楼上了
我的记录是以前在VB一个模块里写了1000行
littlecpu 2003-06-18
  • 打赏
  • 举报
回复
以上两个问题class的代码都超过了2000行。

过程大王!!!!!!!!

62,614

社区成员

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

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