Topic: 继承的问题

  Print this page

1.继承的问题 Copy to clipboard
Posted by: czg4444
Posted on: 2006-09-05 11:56

class Jchen
{
short b=123;
int c=1;
long d=1234567890;
float e=1.333f;
double f= 1111.44d;
}
class JchenB extends Jchen{
public static void main(String args[]){
System.out.println(d+c);
}
}
请高人指教????
谢谢

2.Re:继承的问题 [Re: czg4444] Copy to clipboard
Posted by: 熙城候
Posted on: 2006-09-05 13:42

没有在jchenb中声明 d,c这样编译好像过不了吧

3.Re:继承的问题 [Re: czg4444] Copy to clipboard
Posted by: czg4444
Posted on: 2006-09-05 15:35

java.lang.NoSuchMethodError: main
Exception in thread "main"

4.Re:继承的问题 [Re: czg4444] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2006-09-05 17:27

If you are very novice in learning Java, we can forgive you this question...It is so clear stated in the Java compiler that a non-static variable cannot be used in the static scope like a main method.

---------- Java Compiler ----------
JchenB.java:12: non-static variable d cannot be referenced from a static context
    System.out.println(d+c);
^
JchenB.java:12: non-static variable c cannot be referenced from a static context
    System.out.println(d+c);
^
2 errors

Output completed (1 sec consumed) - Normal Termination

1. try to change your source code like this:
class Jchen
{
  short b=123;
  int c=1;
  long d=1234567890;
  float e=1.333f;
  double f= 1111.44d;
}

class JchenB extends Jchen{
  public static void main(String args[]){
    Jchen test = new JchenB();
    System.out.println(test.d + test.c);
  }
}

2. Save your source code as JchenB.java because it contains your main method.

Notice the code indent because this is a Coding standard.

Kind regards
Jiafan

5.Re:继承的问题 [Re: czg4444] Copy to clipboard
Posted by: leange
Posted on: 2006-09-05 21:06

用super.d+super.c可以嘛??
为什么?

6.Re:继承的问题 [Re: czg4444] Copy to clipboard
Posted by: lisliefor
Posted on: 2006-09-05 22:16

super关键字,是调用父类的方法或变量!
不过同样需要将d和c声明为static,或者你声明一个JChen的对象。

7.Re:继承的问题 [Re: czg4444] Copy to clipboard
Posted by: czg4444
Posted on: 2006-09-07 13:59

大家好
我刚学习到继承
想做个实验
就是建立两个类
A类 建立一些参数

让B类来调用,
我的程序出现了上面的问题???
请高人指教!!!!!!!!!!!!

8.Re:继承的问题 [Re: czg4444] Copy to clipboard
Posted by: yzcj007
Posted on: 2006-09-07 17:07

变量为非static,不可直接使用

class Jchen
{
short b=123;
static int c=1;
static long d=1234567890;
float e=1.333f;
double f= 1111.44d;
}
class JchenB extends Jchen{
public static void main(String args[]){
System.out.println(d+c);
}
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923