使用 Java 继承从其父级获取值
Using Java inheritance for getting value from its parent
我应该使用 extends
关键字从其父变量获取变量值吗?这样合适吗?
我这里有一个代码:
package salarypackageoop;
import java.util.Scanner;
public class mainFunction {
Scanner input = new Scanner(System.in);
double totalOvertime;
double totalHours;
double totalTardiness;
public void timeIn() {
String dayOfwork[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
double getNum = 0;
System.out.println("------------ TIME IN ------------");
for (int i = 0; i < dayOfwork.length; i++) {
System.out.println(dayOfwork[i] + ": ");
getNum = input.nextDouble();
totalHours += getNum;
if (getNum < 8) {
System.out.println("No overtime today!");
totalTardiness += 8 - getNum;
} else if (getNum == 8) {
isValid();
}
}
System.out.println("PAY per hour: 200.50");
System.out.println("---------------------");
System.out.println("Overtime rate: 75.25");
System.out.println("Overtime: " + totalOvertime);
System.out.println("Number of hours: " + totalHours);
System.out.println("-----------------------");
}
public void isValid() {
System.out.println("You can do overtime work if you want!");
System.out.print("How many hours? ");
int getHours = input.nextInt();
totalOvertime += getHours;
}
}
class function2 extends mainFunction {
void display(String name, String id) {
System.out.println("SALARY INFORMATION");
System.out.println("-----------------------");
System.out.println("Employee Id : " + name);
System.out.println("Employee Name : " + id);
System.out.println("Number of hours : " +super.totalHours);
System.out.println("Employee Tardiness: " +super.totalTardiness);
System.out.println("-----------------------");
}
}
我使用 super
关键字从其父级获取 totalHours
和 totalTardiness
的值,但是每当我 运行 程序时, totalHours
和 totalTardiness
没有价值。
这是我调用 类 及其方法的主要方法:
package salarypackageoop;
import java.util.Scanner;
public class SalaryPay {
public static void main(String[] args) {
String getName;
String getId;
Scanner input = new Scanner(System.in);
System.out.println("Calculate Employee salary Per Week");
System.out.println("Input Employee's Data First...");
System.out.print("Employee Name: ");
getName = input.nextLine();
System.out.print("Employee Id: ");
getId = input.nextLine();
employeeData setData = new employeeData();
setData.setEmployeeData(getName, getId);
getName = setData.getEmployeeName();
getId = setData.getEmployeeId();
mainFunction callMeth = new mainFunction();
callMeth.timeIn();
function2 callMeth2 = new function2();
callMeth2.display(getName, getId);
}
}
我的输出是这样的:
Calculate Employee salary Per Week
Input Employee's Data First...
Employee Name: sambajon
Employee Id: emp-212
------------ TIME IN ------------
Monday:
8
You can do overtime work if you want!
How many hours? 2
Tuesday:
8
You can do overtime work if you want!
How many hours? 2
Wednesday:
7
No overtime today!
Thursday:
7
No overtime today!
Friday:
7
No overtime today!
PAY per hour: 200.50
---------------------
Overtime rate: 75.25
Overtime: 4.0
Number of hours: 37.0
-----------------------
SALARY INFORMATION
-----------------------
Employee Id : sambajon
Employee Name : emp-212
Number of hours : 0.0
Employee Tardiness: 0.0
-----------------------
当您想要从现有的 class 获得一些功能,同时又想创建一些新功能时,您可以使用 extends
关键字。
如果要从父项class获取值,需要使用super
关键字,并且变量必须具有protected
范围/可见性。这是示范性的,但它传达了这一点。
class A {
protected int a;
}
class B extends A {
void a() {
super.a = 42;
}
}
您似乎正在创建新对象,并希望填充它们或共享数据。
例如:
mainFunction callMeth = new mainFunction();
callMeth.timeIn();
function2 callMeth2 = new function2();
callMeth2.display(getName, getId);
在这里,您创建了一个名为 callMeth 的 mainFunction class 的新实例。然后调用该对象的 timeIn() 方法。
接下来,您创建函数 2 class 的新实例,命名为 callMeth2。你必须认识到的是,callMeth 和 callMeth2 是完全独立的对象,每个对象都有自己的成员和字段值。
由于 callMeth2 是一个完全独立的对象,实际上没有对它进行任何操作来设置您希望显示的变量。
或许您可以尝试以下方法:
function2 callMeth2 = new function2();
// Initialize the data fields of object callMeth2
callMeth2.timeIn();
// Display callMeth2
callMeth2.display(getName, getId);
这将对同一个对象执行所有操作,因此显示函数将正确初始化要打印的字段。
让我们看看我是否可以添加一个类比。
enum GENDER { male, female, noneoftheabove, etc };
class Human
{
String name;
GENDER gender;
public String getName() { return name; }
public void setName(String nm) { this.name = nm; }
}
这里有一个人 class,人的对象 class 有名字和性别。
class Wizard extends Human
{
boolean parselmouth;
public String getNameAndGender() { return name + gender; }
}
巫师class从人类延伸而来,因为他们都有名字和性别,但他们也可能是蛇佬腔。
要使用这些 classes,您需要创建所需类型的对象。例如,
Human dudley = new Human();
dudley.setName("Dudley Dursley");
Wizard harry = new Wizard();
harry.setName("Harry Potter");
harry.parselmouth = true;
您正在做的类似于以下内容:
// Create a Human
Human h = new Human();
h.setName("Dudley Dursley");
h.gender = GENDER.male;
// Create a Wizard
Wizard wiz = new Wizard();
// wiz is not initialized in any way
// Print out the name and gender of wiz:
System.out.println(wiz.getNameAndGender());
并期望输出为“Dudley Dursley male”。
我应该使用 extends
关键字从其父变量获取变量值吗?这样合适吗?
我这里有一个代码:
package salarypackageoop;
import java.util.Scanner;
public class mainFunction {
Scanner input = new Scanner(System.in);
double totalOvertime;
double totalHours;
double totalTardiness;
public void timeIn() {
String dayOfwork[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
double getNum = 0;
System.out.println("------------ TIME IN ------------");
for (int i = 0; i < dayOfwork.length; i++) {
System.out.println(dayOfwork[i] + ": ");
getNum = input.nextDouble();
totalHours += getNum;
if (getNum < 8) {
System.out.println("No overtime today!");
totalTardiness += 8 - getNum;
} else if (getNum == 8) {
isValid();
}
}
System.out.println("PAY per hour: 200.50");
System.out.println("---------------------");
System.out.println("Overtime rate: 75.25");
System.out.println("Overtime: " + totalOvertime);
System.out.println("Number of hours: " + totalHours);
System.out.println("-----------------------");
}
public void isValid() {
System.out.println("You can do overtime work if you want!");
System.out.print("How many hours? ");
int getHours = input.nextInt();
totalOvertime += getHours;
}
}
class function2 extends mainFunction {
void display(String name, String id) {
System.out.println("SALARY INFORMATION");
System.out.println("-----------------------");
System.out.println("Employee Id : " + name);
System.out.println("Employee Name : " + id);
System.out.println("Number of hours : " +super.totalHours);
System.out.println("Employee Tardiness: " +super.totalTardiness);
System.out.println("-----------------------");
}
}
我使用 super
关键字从其父级获取 totalHours
和 totalTardiness
的值,但是每当我 运行 程序时, totalHours
和 totalTardiness
没有价值。
这是我调用 类 及其方法的主要方法:
package salarypackageoop;
import java.util.Scanner;
public class SalaryPay {
public static void main(String[] args) {
String getName;
String getId;
Scanner input = new Scanner(System.in);
System.out.println("Calculate Employee salary Per Week");
System.out.println("Input Employee's Data First...");
System.out.print("Employee Name: ");
getName = input.nextLine();
System.out.print("Employee Id: ");
getId = input.nextLine();
employeeData setData = new employeeData();
setData.setEmployeeData(getName, getId);
getName = setData.getEmployeeName();
getId = setData.getEmployeeId();
mainFunction callMeth = new mainFunction();
callMeth.timeIn();
function2 callMeth2 = new function2();
callMeth2.display(getName, getId);
}
}
我的输出是这样的:
Calculate Employee salary Per Week
Input Employee's Data First...
Employee Name: sambajon
Employee Id: emp-212
------------ TIME IN ------------
Monday:
8
You can do overtime work if you want!
How many hours? 2
Tuesday:
8
You can do overtime work if you want!
How many hours? 2
Wednesday:
7
No overtime today!
Thursday:
7
No overtime today!
Friday:
7
No overtime today!
PAY per hour: 200.50
---------------------
Overtime rate: 75.25
Overtime: 4.0
Number of hours: 37.0
-----------------------
SALARY INFORMATION
-----------------------
Employee Id : sambajon
Employee Name : emp-212
Number of hours : 0.0
Employee Tardiness: 0.0
-----------------------
当您想要从现有的 class 获得一些功能,同时又想创建一些新功能时,您可以使用 extends
关键字。
如果要从父项class获取值,需要使用super
关键字,并且变量必须具有protected
范围/可见性。这是示范性的,但它传达了这一点。
class A {
protected int a;
}
class B extends A {
void a() {
super.a = 42;
}
}
您似乎正在创建新对象,并希望填充它们或共享数据。 例如:
mainFunction callMeth = new mainFunction();
callMeth.timeIn();
function2 callMeth2 = new function2();
callMeth2.display(getName, getId);
在这里,您创建了一个名为 callMeth 的 mainFunction class 的新实例。然后调用该对象的 timeIn() 方法。
接下来,您创建函数 2 class 的新实例,命名为 callMeth2。你必须认识到的是,callMeth 和 callMeth2 是完全独立的对象,每个对象都有自己的成员和字段值。
由于 callMeth2 是一个完全独立的对象,实际上没有对它进行任何操作来设置您希望显示的变量。
或许您可以尝试以下方法:
function2 callMeth2 = new function2();
// Initialize the data fields of object callMeth2
callMeth2.timeIn();
// Display callMeth2
callMeth2.display(getName, getId);
这将对同一个对象执行所有操作,因此显示函数将正确初始化要打印的字段。
让我们看看我是否可以添加一个类比。
enum GENDER { male, female, noneoftheabove, etc };
class Human
{
String name;
GENDER gender;
public String getName() { return name; }
public void setName(String nm) { this.name = nm; }
}
这里有一个人 class,人的对象 class 有名字和性别。
class Wizard extends Human
{
boolean parselmouth;
public String getNameAndGender() { return name + gender; }
}
巫师class从人类延伸而来,因为他们都有名字和性别,但他们也可能是蛇佬腔。
要使用这些 classes,您需要创建所需类型的对象。例如,
Human dudley = new Human();
dudley.setName("Dudley Dursley");
Wizard harry = new Wizard();
harry.setName("Harry Potter");
harry.parselmouth = true;
您正在做的类似于以下内容:
// Create a Human
Human h = new Human();
h.setName("Dudley Dursley");
h.gender = GENDER.male;
// Create a Wizard
Wizard wiz = new Wizard();
// wiz is not initialized in any way
// Print out the name and gender of wiz:
System.out.println(wiz.getNameAndGender());
并期望输出为“Dudley Dursley male”。