java - 封装构造函数错误

标签 java constructor encapsulation

我使用封装(数据隐藏)将代码放在 3 个不同的文件中,当尝试从其他 2 个文档调用类时,我的 if 和 else 语句(非常底部)中的代码末尾有 1 个问题。我将把代码放在第一个文档到第三个文档中。对我做错了什么有什么建议吗?

// FIRST DOCUMENT
public class CollegeCourse { //class name

//variables
String deptName;
int courseNum;
int credits = 3;
double fee;

  //constructor
  public CollegeCourse(String department, int course, int Credits) {

     deptName = department;
     courseNum = course;
     credits = Credits;
     fee = credits * 120;
  }

  //getters setters
  public String getdepartment() {
     return deptName;      
  }
  public String setdepartment(String dept) {
     return dept = deptName;      
  }
  public int getcourse() {
     return courseNum;      
  }
  public int setcourse(int c) {
     return c = courseNum;      
  }
  public int getCredits() {
     return credits;      
  }      
  public int setCredits(int cred) {
     return cred = credits;      
  }

  public void display()
  {
  System.out.println("Department: " + deptName);
  System.out.println("Course Number: " + courseNum);
  System.out.println("Credits: " + credits);
  System.out.println("Fee: $" + fee);
  }
}
//SECOND DOCUMENT
public class LabCourse extends CollegeCourse { //polymorphism extending         CollegeCourse class into LabCourse class. 

//constructor
public LabCourse(String department, int course, int Credits){
//add 50 dollars to the fee 
  super(department, course, Credits);
  fee = fee + 50;
}
//display the course
  public void display(){
  System.out.print("This course is a lab course" + fee);
  System.out.println("Department: " + deptName);
  System.out.println("Course Number: " + courseNum);
  System.out.println("Credits: " + credits);
  System.out.println("Fee: $" + fee);
  }
}    
//THIRD DOCUMENT MAIN HEADER
import java.util.Scanner;
public class UseCourse {

public static void main(String[] args){ 

  String s, c, cd;

  Scanner input = new Scanner(System.in);
  System.out.print("Enter: BIO, CHEM, ENG, MATH: ");
  s = input.nextLine();   

  System.out.print("What is the course number: ");
  c = input.nextLine();      

  System.out.print("How many credits: ");
  cd = input.nextLine();      

  if(s.equals ("BIO") || s.equals ("CHEM")){
     LabCourse lc = new LabCourse(department, course, Credits); //here is my problem, it  can't find the CollegeCourse class department, course,//and credits...
     lc.display();       
  }

  else {
     CollegeCourse cc = new CollegeCourse(department, course, Credits); //here is my problem, it  can't find the CollegeCourse class department, course,//and credits...
     cc.display();
  }

   }  
 }

这是我遇到的错误。

UseCourse.java:24: error: cannot find symbol
     LabCourse lc = new LabCourse(department, course, Credits);
                                  ^

并且对于每个错误“部门、类(class)、学分”都会重复

UseCourse.java:29: error: cannot find symbol
     CollegeCourse cc = new CollegeCourse(department, course, Credits);
                                                      ^

最佳答案

你的构造函数调用中的参数都是错误的。 departmentcourseCredits 均未定义,因此您需要使用 sccd 而是,因为这些是您用于输入的变量。

此外,您需要将 ccd 作为整数读取,并将它们传递给构造函数,如下所示:

System.out.print("What is the course number: ");
int c = input.nextInt();

System.out.print("How many credits: ");
int cd = input.nextInt();

// ...

LabCourse lc = new LabCourse(s, c, cd);

关于java - 封装构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189951/

相关文章:

java - Google App Engine (Java) 中的持久性 - 访问应用程序外部的数据

java - Inheritance中关于NEW Keyword的打印顺序是怎样的?

.net - 只读可观察集合

c - C中如何实现封装

java - "Exception in thread "main "java.lang.NullPointerException"运行网页抓取程序时出错

Java执行流程?

java - wait() 不会释放所有当前锁,如何实现?

python - 如何在 Python 中调用 super 构造函数?

c++ - 为什么在这种情况下不调用复制构造函数?

Java数据字段封装newby问题