首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试仪程序错误

测试仪程序错误
EN

Stack Overflow用户
提问于 2019-03-20 02:32:02
回答 2查看 51关注 0票数 0

我已经为这个程序写了一个程序和测试器,但是当我编译测试器时,我得到了一些错误(如下所示),有人能理解我为什么得到这些错误吗?

主代码-

代码语言:javascript
复制
package com.date.example;
import java.io.*;
import java.util.*;

public class Student {
public static void main(String[] args) {

    Student student = new Student("Charles");
    }

    private String forName;
    private String surName;
    private String studentID;
    private String degreeScheme;

    //This is the Constructor of the 
    public Student(String forName) {
        this.forName = forName;
    }

    public Student() {
        // TODO Auto-generated constructor stub
    }

    //Assign the surname of the student 
    public void stuSurname(String stuSurname) {
        surName = stuSurname;
    }

    //Assign the student ID to the student
    public void stuID(String stuID) {
        studentID = stuID;
    }

    //Assign the Degree of the Student
    public void stuDegree(String stuDegree) {
        degreeScheme = stuDegree;
    }

    //Print the student details
    public void printStudent() {
        System.out.println("Forname:" + forName);
        System.out.println("Surename:" + 
surName);
        System.out.println("Student ID:" + 
studentID);
        System.out.println("Degree Scheme:" + 
degreeScheme);
    }

    // setter
    public void setForName(String forName) {
        this.forName = forName;
    }

    // getter
    public String getForName() {
        return forName;
    }
}

测试器程序-

代码语言:javascript
复制
package com.date.example;
import java.io.*;

public class StudentTest {

public static void main(String[] args) {
        /*create three new objects using constructor*/
    Student stuOne = newStudent1();
    Student stuTwo = newStudent2();
    Student stuThree = newStudent3();

    //Invoking Methods for Each object Created
    stuOne.setForName("James");
    stuOne.stuSurname("Smith");
    stuOne.stuID("0987");
    stuOne.stuDegree("Computer Science");

    stuTwo.setForName("Vanessa");
    stuTwo.stuSurname("Peach");
    stuTwo.stuID("0988");
    stuTwo.stuDegree("Mathematics");

    stuThree.setForName("George");
    stuThree.stuID("0989");
    stuThree.stuDegree("English");
//Invoking the printStudentmethod.
    stuOne.printStudent();
    System.out.println("\n");
    stuTwo.printStudent();
    System.out.println("\n");
    stuThree.printStudent();

   }
 }

这就是编写代码的目的-

类Student应该包含一个构造函数、适当的getter和setter,以及常用的字符串方法。编译java源代码以获得.class文件,然后编写一个测试程序类来创建三个学生实例。在本练习中,提供学生详细信息作为硬编码参数。一如既往,确保您的测试程序提供100%的方法覆盖率。

然后这是我得到的编译错误-

代码语言:javascript
复制
TheRealFawcett:Lab8 therealfawcett$ javac StudentTest.java
StudentTest.java:8: error: cannot find symbol
    Student stuOne = newStudent1();
                 ^
  symbol:   class Student
  location: class StudentTest
StudentTest.java:8: error: cannot find symbol
    Student stuOne = newStudent1();
                 ^
  symbol:   method newStudent1()
  location: class StudentTest
StudentTest.java:9: error: cannot find symbol
    Student stuTwo = newStudent2();
                 ^
  symbol:   class Student
  location: class StudentTest
StudentTest.java:9: error: cannot find symbol
    Student stuTwo = newStudent2();
                 ^
  symbol:   method newStudent2()
  location: class StudentTest
StudentTest.java:10: error: cannot find symbol
    Student stuThree = newStudent3();
                   ^
  symbol:   class Student
  location: class StudentTest
StudentTest.java:10: error: cannot find symbol
    Student stuThree = newStudent3();
                   ^
  symbol:   method newStudent3()
  location: class StudentTest
6 errors
TheRealFawcett:Lab8 therealfawcett$ 

如果任何人理解为什么我会得到这些错误的帮助,我将非常感激,我是java的新手,这就是我所得到的。

EN

回答 2

Stack Overflow用户

发布于 2019-03-20 02:45:12

可能是因为您正在发明自己的Java语法:

代码语言:javascript
复制
Student stuOne = newStudent1();

应该是:

代码语言:javascript
复制
 Student stuOne = new Student();

就像您在类中的主方法中所做的那样。

除此之外,真正的答案是:不要写这么多代码,然后最终运行编译器。只需编写几行代码,就足以让您认为“这应该会编译”。然后运行编译器。修复所有的bug。再写几行。诸若此类。

除此之外,这里真正的问题可能是您不了解在使用javac编译包中的类时需要遵循的复杂规则。例如,我建议仔细阅读这个tutorial

票数 2
EN

Stack Overflow用户

发布于 2021-11-11 06:30:32

我看到的两个错误:

实际上,要在类中创建对象,您必须使用类对象,即 StudentTest ,而不是StudentTest stuTwo = newStudent2();,如果您没有使用StudentTest,也就是学生,那么对象创建应该是从哪里开始的?如果你想使用main类中的对象?您必须使用继承来获取对象,否则新对象的创建将不会引用它。假设问题是创建新对象时找不到其符号。

对于新的Student();,

  1. newStudent();->应该是->

结论要使用主类对象,应使用继承的方法。请找到有关继承的教程

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55247841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档