Java extends inheritance 程序不工作?
Java extends inheritance Program not working?
所以我有 3 个文件 - 在其中一个文件中出现此错误:
Package in class Package cannot be applied to given types; required
int, char / Found no arguments
我已经尝试了所有可能想到的方法,但无法弄清楚如何修复它,这可能是菜鸟错误!?所以我希望有人能帮助解释我做错了什么。我会在下面粘贴代码!
Package.java
package chap10q5;
/**
*
* @author james
*/
public class Package {
int weight;
char shippingMethod;
double shippingcost;
public Package(int weight, char shippingMethod){
this.weight = weight;
this.shippingMethod = shippingMethod;
if( (this.weight > 0 && this.weight <= 8) && this.shippingMethod == 'A'){
this.shippingcost = 2.00;
}
else if( (this.weight > 0 && this.weight <= 8) && this.shippingMethod == 'T'){
this.shippingcost = 1.50;
}
else if( (this.weight > 0 && this.weight <= 8) && this.shippingMethod == 'M'){
this.shippingcost = 0.50;
}
if( (this.weight >=9 && this.weight <= 16) && this.shippingMethod == 'A'){
this.shippingcost = 3.00;
}
else if( (this.weight >= 9 && this.weight <= 16) && this.shippingMethod == 'T'){
this.shippingcost = 2.35;
}
else if( (this.weight >= 9 && this.weight <= 16) && this.shippingMethod == 'M'){
this.shippingcost = 1.50;
}
if( (this.weight > 17) && this.shippingMethod == 'A'){
this.shippingcost = 4.50;
}
else if( (this.weight > 17) && this.shippingMethod == 'T'){
this.shippingcost = 3.25;
}
else if( (this.weight > 17) && this.shippingMethod == 'M'){
this.shippingcost = 2.15;
}
}
public String toString(){
return "Weight: " + this.weight + "\nShipping Method: " +
this.shippingMethod + "\nCost: " + this.shippingcost;
}
}
投保Package.java
package chap10q5;
/**
*
* @author james
*/
public class InsuredPackage extends Package {
public InsuredPackage(int weight, char shippingCost){
if(super.shippingcost <= 1){
super.shippingcost += 2.45;
}
else if( (super.shippingcost > 1.00) && (super.shippingcost <= 3.00) ){
super.shippingcost += 3.95;
}
else if( super.shippingcost > 3 ){
super.shippingcost += 5.55;
}
}
@Override
public String toString(){
return super.toString();
}
}
使用Package.java(我的主要方法):
package chap10q5;
/**
*
* @author james
*/
public class UsePackage {
public static void main(String[] args){
Package one = new Package(1, 'A');
System.out.println(one);
}
}
由于您在 Package
中没有默认构造函数,您需要在 InsuredPackage
构造函数中调用 Package
的构造函数之一。例如:
public InsuredPackage(int weight, char shippingMethod, double shippingCost) {
super(weight, shippingMethod);
...
}
改变
public InsuredPackage(int weight, char shippingCost){
if(super.shippingcost <= 1){
super.shippingcost += 2.45;
}
else if( (super.shippingcost > 1.00) && (super.shippingcost <= 3.00) ){
super.shippingcost += 3.95;
}
else if( super.shippingcost > 3 ){
super.shippingcost += 5.55;
}
}
至
public InsuredPackage(int weight, char shippingMethod){
super(weight,shippingMethod);
if(shippingcost <= 1){
shippingcost += 2.45;
}
else if( (shippingcost > 1.00) && (shippingcost <= 3.00) ){
shippingcost += 3.95;
}
else if(shippingcost > 3 ){
shippingcost += 5.55;
}
}
问题在于,当子 class 的构造函数不调用父 class 的构造函数时,编译器会在第一行调用默认构造函数,我的意思是 super()
,但您没有在 Package
class 中定义它,这就是错误。
所以我有 3 个文件 - 在其中一个文件中出现此错误:
Package in class Package cannot be applied to given types; required int, char / Found no arguments
我已经尝试了所有可能想到的方法,但无法弄清楚如何修复它,这可能是菜鸟错误!?所以我希望有人能帮助解释我做错了什么。我会在下面粘贴代码!
Package.java
package chap10q5;
/**
*
* @author james
*/
public class Package {
int weight;
char shippingMethod;
double shippingcost;
public Package(int weight, char shippingMethod){
this.weight = weight;
this.shippingMethod = shippingMethod;
if( (this.weight > 0 && this.weight <= 8) && this.shippingMethod == 'A'){
this.shippingcost = 2.00;
}
else if( (this.weight > 0 && this.weight <= 8) && this.shippingMethod == 'T'){
this.shippingcost = 1.50;
}
else if( (this.weight > 0 && this.weight <= 8) && this.shippingMethod == 'M'){
this.shippingcost = 0.50;
}
if( (this.weight >=9 && this.weight <= 16) && this.shippingMethod == 'A'){
this.shippingcost = 3.00;
}
else if( (this.weight >= 9 && this.weight <= 16) && this.shippingMethod == 'T'){
this.shippingcost = 2.35;
}
else if( (this.weight >= 9 && this.weight <= 16) && this.shippingMethod == 'M'){
this.shippingcost = 1.50;
}
if( (this.weight > 17) && this.shippingMethod == 'A'){
this.shippingcost = 4.50;
}
else if( (this.weight > 17) && this.shippingMethod == 'T'){
this.shippingcost = 3.25;
}
else if( (this.weight > 17) && this.shippingMethod == 'M'){
this.shippingcost = 2.15;
}
}
public String toString(){
return "Weight: " + this.weight + "\nShipping Method: " +
this.shippingMethod + "\nCost: " + this.shippingcost;
}
}
投保Package.java
package chap10q5;
/**
*
* @author james
*/
public class InsuredPackage extends Package {
public InsuredPackage(int weight, char shippingCost){
if(super.shippingcost <= 1){
super.shippingcost += 2.45;
}
else if( (super.shippingcost > 1.00) && (super.shippingcost <= 3.00) ){
super.shippingcost += 3.95;
}
else if( super.shippingcost > 3 ){
super.shippingcost += 5.55;
}
}
@Override
public String toString(){
return super.toString();
}
}
使用Package.java(我的主要方法):
package chap10q5;
/**
*
* @author james
*/
public class UsePackage {
public static void main(String[] args){
Package one = new Package(1, 'A');
System.out.println(one);
}
}
由于您在 Package
中没有默认构造函数,您需要在 InsuredPackage
构造函数中调用 Package
的构造函数之一。例如:
public InsuredPackage(int weight, char shippingMethod, double shippingCost) {
super(weight, shippingMethod);
...
}
改变
public InsuredPackage(int weight, char shippingCost){
if(super.shippingcost <= 1){
super.shippingcost += 2.45;
}
else if( (super.shippingcost > 1.00) && (super.shippingcost <= 3.00) ){
super.shippingcost += 3.95;
}
else if( super.shippingcost > 3 ){
super.shippingcost += 5.55;
}
}
至
public InsuredPackage(int weight, char shippingMethod){
super(weight,shippingMethod);
if(shippingcost <= 1){
shippingcost += 2.45;
}
else if( (shippingcost > 1.00) && (shippingcost <= 3.00) ){
shippingcost += 3.95;
}
else if(shippingcost > 3 ){
shippingcost += 5.55;
}
}
问题在于,当子 class 的构造函数不调用父 class 的构造函数时,编译器会在第一行调用默认构造函数,我的意思是 super()
,但您没有在 Package
class 中定义它,这就是错误。