public class Employee {
String name;
char sex;
Employee(String n,char s){
name = n;
sex = s;
}
public String getName(){
return name;
}
public char getSex(){
return sex;
}
class Worker extends Employee{
char category;
boolean dressAllowance;
Worker(String n,char s,char c,boolean d){
super(n,s);
category = c;
dressAllowance = d;
}
public char getCategory(){
return category;
}
public boolean getAll(){
return dressAllowance;
}
}
class InheDemo{
public static void main(String args[]){//main报错
Worker w = new Worker("JAVALIU",'M','B',true);
System.out.println("工人信息");
System.out.println("姓名 :"+w.getName());
System.out.println("性别 :"+w.getSex());
System.out.println("类别 :"+w.getCategory());
if(w.getAll())
System.out.println("提供服装津贴");
else
System.out.println("未提供服装津贴");
}
}
}请问是什么原因呢?
String name;
char sex;
Employee(String n,char s){
name = n;
sex = s;
}
public String getName(){
return name;
}
public char getSex(){
return sex;
}
class Worker extends Employee{
char category;
boolean dressAllowance;
Worker(String n,char s,char c,boolean d){
super(n,s);
category = c;
dressAllowance = d;
}
public char getCategory(){
return category;
}
public boolean getAll(){
return dressAllowance;
}
}
class InheDemo{
public static void main(String args[]){//main报错
Worker w = new Worker("JAVALIU",'M','B',true);
System.out.println("工人信息");
System.out.println("姓名 :"+w.getName());
System.out.println("性别 :"+w.getSex());
System.out.println("类别 :"+w.getCategory());
if(w.getAll())
System.out.println("提供服装津贴");
else
System.out.println("未提供服装津贴");
}
}
}请问是什么原因呢?
不然不能访问其它非静态的成员.此做为一个内部类,不能有静态声明,所以要把这个带有main的类放到外面.
放外面以后再来访问内部的Worker类Employee em = new Employee("20",'m');
Employee.Worker w = em.new Worker("JAVALIU",'M','B',true);
不然不能访问其它非静态的成员.
再说做为做为一个内部类,不能有静态声明,所以要把这个带有main的类放到外面.
放外面以后再来访问内部的Worker类 Employee em = new Employee("20",'m');
Employee.Worker w = em.new Worker("JAVALIU",'M','B',true);
1、不能在内部类里写静态方法,所以把mian()方法放到Employee下,相应的class InheDemo也就没意义了;
2、main()方法是静态的,无法引用非静态的内部类直接构造对象,加个“static” class Worker extends Employee;
3、创建对象时候Employee.Worker w=new Employee.Worker();至于其他解决方法,没有找到,希望能从高手继续学习。谢谢
class Employee {
String name;
char sex;
Employee(String n,char s){
name = n;
sex = s;
}
public String getName(){
return name;
}
public char getSex(){
return sex;
}
public static void main(String args[]){
Worker w = new Worker("JAVALIU",'M','B',true);
System.out.println("工人信息");
System.out.println("姓名 :"+w.getName());
System.out.println("性别 :"+w.getSex());
System.out.println("类别 :"+w.getCategory());
if(w.getAll())
System.out.println("提供服装津贴");
else
System.out.println("未提供服装津贴");
}
}
class Worker extends Employee{
char category;
boolean dressAllowance;
Worker(String n,char s,char c,boolean d){
super(n,s);
category = c;
dressAllowance = d;
}
public char getCategory(){
return category;
}
public boolean getAll(){
return dressAllowance;
}
}
2.主方法不能写在内部类中.(编译器报错看出来的)
4.主方法的类要是static(自己调试出来的以前也没注意过)
综合上面所说.把代码改成如下...
文件名是InheDemo.javaclass Employee {
String name;
char sex;
Employee(String n,char s){
name = n;
sex = s;
}
public String getName(){
return name;
}
public char getSex(){
return sex;
}
}
class Worker extends Employee{
char category;
boolean dressAllowance;
Worker(String n,char s,char c,boolean d){
super(n,s);
category = c;
dressAllowance = d;
}
public char getCategory(){
return category;
}
public boolean getAll(){
return dressAllowance;
}
}
public class InheDemo{
public static void main(String args[]){//main报错
Worker w = new Worker("JAVALIU",'M','B',true);
System.out.println("工人信息");
System.out.println("姓名 :"+w.getName());
System.out.println("性别 :"+w.getSex());
System.out.println("类别 :"+w.getCategory());
if(w.getAll())
System.out.println("提供服装津贴");
else
System.out.println("未提供服装津贴");
}
}楼主出错在两个地方.class Employee的右括号.写在了最后应该写在Worker类的上面.
第二个错误是主方法的类是static来定义的..剩下的没有了代码测试通过下面是代码结果:工人信息
姓名 :JAVALIU
性别 :M
类别 :B
提供服装津贴
给分吧楼主加油..我上了这么久一次分都没得过
4.主方法的类要是static(自己调试出来的以前也没注意过)
更正为
4.主方法的类要是public (自己调试出来的以前也没注意过)