public class person {
String s1;
int age;
person()
{
s1="wjjkknd";
age=22;
}
public void setage(int t)
{
age=t;
}
public int printage()
{
System.out.println(age);
} public static void main(String[] args)
{
person a;
a.printage;
}
}
String s1;
int age;
person()
{
s1="wjjkknd";
age=22;
}
public void setage(int t)
{
age=t;
}
public int printage()
{
System.out.println(age);
} public static void main(String[] args)
{
person a;
a.printage;
}
}
public static void main(String[] args)
{
person a=new person();
a.printage();
}
}
class person {
String s1;
int age;
person()
{
s1="wjjkknd";
age=22;
}
public void setage(int t)
{
age=t;
}
public void printage()
{
System.out.println(age);
}
}
但编译器 还是说, main 无法调用非静态的
{
public static void main(String[] args)
{
wjk a=new wjk();
a.printage();
}
public printage()
{
System.out.println("age");
}
}
String s1;
int age;
person()
{
s1="wjjkknd";
age=22;
}
public void setage(int t)
{
age=t;
}
public void printage()
{
System.out.println(age);
} public static void main(String[] args)
{
person a = new person();
a.printage();
}
}
无法在 main 中, 用自己定义的类,
public class wjk {
public static void main(String[] args)
{
person a =new person(); a.printage();
}
}
class person {
String s1;
int age;
person()
{
s1="wjjkknd";
age=22;
}
public void setage(int t)
{
age=t;
}
public void printage()
{
System.out.println(age);
}
}
后 会说:
Exception in thread "main" java.lang.NoclassDefFoundError: person
很明显,有九成是你JDK路径设置的问题.请先设置classpath
String s1;
int age;
public void person()
{
s1="wjjkknd";
age=22;
}
public void setage(int t)
{
age=t;
}
public void printage()
{
System.out.print(s1);
System.out.println(age);
}
public static void main(String[] args)
{
wjk a=new wjk();
a.printage();
}
}
public class wjk {
String s1;
int age;
public void wjk()
{
s1="wjjkknd";
age=22;
}
public void setage(int t)
{
age=t;
}
public void printage()
{
System.out.print(s1);
System.out.println(age);
}
public static void main(String[] args)
{
wjk a=new wjk();
a.printage();
}
}
能---再说
这样可以实现你要求的输出一个人名,一个年龄:
public class wjk {
String s1;
int age;
wjk()
{
s1="wjjkknd";
age=22;
}
public void setage(int t)
{
age=t;
}
public void printage()
{
System.out.println(s1);
System.out.println(age);
}
public static void main(String[] args)
{
wjk a=new wjk();
a.printage();
}
}
person a=new person();
a.printage();
}
public class hello
{
public static void main(String[] args)
{
System.out.println("hello word!");
}
}
提示:Exception in thread "main" java.lang.NoclassDefFoundError: hello
你是不是直接安的j2sdk?如果是,你就直接使用它自己给你配置的path把classpath删了,就行了,我就是。
* @(#)person.java 1.70 01/12/03
*
* Copyright 2004 cfzq, Inc. All rights reserved.
*/
/**
* @author WJJmooor (光复者) ,edited byFane
* @version 1.70, 10/13/04
* @since JDK1.4.0
*/
class wjk {
public static void main(String[] args)
{
person a=new person();
a.printage();
}
}public class person {
String s1;
int age;
person()
{
s1="wjjkknd";
age=22;
}
//method for set age
public void setage(int t)
{
age=t;
}
//print the age
public void printage()
{
System.out.println(age);
}
}
这个代码可以用了,楼猪,记得声明一个实例
在一个文件里只能有一个public类,所以去掉以上wjk和person的任何一个puublic
代码上加上详细的注释是很好的习惯
方法public void printage()前加int需要返回值
还有:不知道楼猪怎么编译的,不会提示static问题,而是命名和类实例声明错误的问题最后,也是最重要的,及时结帖是对帮助你的人的最好的支持
public static void main(String[] args)
{
Person a=new Person();
System.out.println(a.getAge());
}
} class Person {
String s1;
public int age;
Person()
{
s1="wjjkknd";
age=22;
}
//method for set age
public void setAge(int age)
{
this.age=age;
}
//print the age
public int getAge()
{
return age;
}
}
文件名Wjk.java
person a=new person();
a.printage;
再编译wjk.java,就可以运行了。
person a必须写成person a=new person()