gpt4 book ai didi

java - 无法解析为 Java 中基类的变量

转载 作者:行者123 更新时间:2023-12-02 13:24:50 26 4
gpt4 key购买 nike

我是java新手,正在创建一个小程序来清除核心java的基本原理。我正在创建一个员工基本信息和工资计数的程序。但是,当我尝试访问父变量以便使用该变量并计算员工的工资时,却收到一条错误消息:“empSalaryobj 无法解析为变量”

下面是我的 Java 代码,

class varEmployee {
int empId;
String empName;
final String empCompany = "Tata Consultancy Services";
int empSalary;
String empGender;
int empAge;
String empNationality;
}

class setEmployee extends varEmployee{


setEmployee(){

}

setEmployee(int empId){
this.empId=empId;
switch(empId){

case 1:
empName = "Aditya Batra";
empSalary = 10000;
empGender = "Male";
empAge = 26;
empNationality = "Indian";
break;

case 2:
empName = "Vishal Sharma";
empSalary = 20500;
empGender = "Male";
empAge = 26;
empNationality = "Australia";
break;

case 3:
empName = "Ashish Tiwari";
empSalary = 30000;
empGender = "Male";
empAge = 28;
empNationality = "Indian";
break;

case 4:
empName = "Mona Lisha";
empSalary = 40000;
empGender = "Female";
empAge = 24;
empNationality = "Oganda";
break;

default:

System.out.println("Invalid Employee ID");
}
}

}

class getEmployee extends setEmployee {

getEmployee(){

}

getEmployee(setEmployee obj){

int empIdobj = obj.empId;
String empNameobj = obj.empName;
int empSalaryobj = obj.empSalary;
String empGenderobj = obj.empGender;
int empAgeobj = obj.empAge;
String empNationalityobj = obj.empNationality;

System.out.println("Employee Id: "+empIdobj+"\nName: "
+empNameobj+"\nSalary: "+empSalaryobj
+"\nGender: "+empGenderobj+"\nAge: "
+empAgeobj+"\nNationality: "+empNationalityobj
+"\nCompany Name: "+empCompany);

}
}

class getEmployeeSalary extends getEmployee {

int empSalaryCaculation = empSalaryobj;

getEmployeeSalary(int empLeaves){
float empSal = (empSalaryCaculation/30)*(30-empLeaves);
System.out.println("Salary after leave deduction: "+empSal);
}

}

public class Employee {

public static void main(String[] args) {
setEmployee setobj=new setEmployee(1);
getEmployee getobj=new getEmployee(setobj);
getEmployeeSalary salobj=new getEmployeeSalary(5);
}

}

有人可以帮帮我吗?我也想知道为什么会出现这个错误信息。

最佳答案

empSalaryobj 是在 getEmployee 的构造函数中定义的局部变量。它不是 getEmployeeSalary 任何父类的字段,因此无法在 getEmployeeSalary 的构造函数中访问它。

关于java - 无法解析为 Java 中基类的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43433679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com