layout是java中的一种程序结构

d6kp6zgx  于 2021-07-09  发布在  Java
关注(0)|答案(5)|浏览(290)

这个问题不太可能帮助任何未来的游客;它只与一个小的地理区域、一个特定的时刻或一个非常狭窄的情况有关,而这些情况通常不适用于互联网的全球受众。有关使此问题更广泛适用的帮助,请访问帮助中心。
8年前关门了。
我的程序建设有问题。我似乎找不到我需要把构造函数放在哪里或为什么。我不知道它在不在里面。总之,这里是主要代码:

import java.io.FileNotFoundException;
import java.util.Scanner;

public class HangmanProject
{
    public static void main(String[] args) throws FileNotFoundException
    {
        public static void getFile() {

    getFile gf() = new getFile();
    Scanner test = gf.wordScan;      
   }     
}

这是主程序,但它称之为:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

public class getFile 
          {  
    String wordList[] = new String[10];    // array to store all of the words
    int x = 0;

    Scanner keyboard = new Scanner(System.in);    // to read user's input

    System.out.println("Welcome to Hangman Project!");

                                 // Create a scanner to read the secret words file
    Scanner wordScan = null;

    try {
        wordScan = new Scanner(new BufferedReader(new FileReader("words.txt")));
        while (wordScan.hasNext()) {
            wordList[x] = wordScan.next();  
            System.out.println(wordList[x]);  
            x++;  
                                   }
        } 
    finally {
        if (wordScan != null)
        {
            wordScan.close();
        }
    }
}

我的问题是:
我的构装师呢,
我用对了吗,
我的布局应该改变吗?
我的指导老师告诉我“我仍然没有在你的类中看到构造函数方法,你应该在那里初始化你的类的示例变量。你不能只是把代码放在一个类中,“我真的不明白这意味着什么。

yjghlzjz

yjghlzjz1#

您可以假设构造函数是一个特殊的方法,其名称与class相同,并且没有返回类型。
如果类名是 HangmanProject 构造函数应该是

public HangmanProject(){
}

第二,它应该在类中,这是一个构造函数。。。

class HangmanProject{
    public HangmanProject(){
    }
}

最后还可以取参数;你可以重载它们,也就是说,你可以在一个类中有多个构造函数。

class HangmanProject{
    Integer attemptsLeft;
    public HangmanProject(){
        attemptsLeft = 10;
    }

    public HangmanProject(Integer attempts){
        this.attemptsLeft = attempts;
    }

}

构造函数的用途也是初始化类的字段。
作为你的另一个班级的例子。。。

public class GetFile {    //  Changed as per naming convention...

     String wordList[];
     int x;
     Scanner keyboard = null;    // to read user's input
     Scanner wordScan = null;

     public GetFile(){     // Here is the constructor
         this.wordList = new String[10];
         this.x=0;
     }

}

希望这对你有帮助!!!

7cjasjjr

7cjasjjr2#

不同的java程序员可以有不同的编程方式和方法。通过使用标准的java命名约定,它们使自己和其他程序员的代码更易于阅读。java代码的可读性很重要,因为这意味着花更少的时间来试图弄清楚代码的功能,而留下更多的时间来修复或修改它。
为了说明这一点,值得一提的是,大多数软件公司都会有一个文档,概述他们希望程序员遵循的命名约定。熟悉这些规则的新程序员将能够理解程序员编写的代码,这些程序员可能已经离开公司很多年了。
java编程语言的代码约定

yzckvree

yzckvree3#

首先,我对你的程序做了一些修改。第一个大错误是有一个名为getfile的类,还有一个名为getfile的方法。保持方法名为方法名,但类名通常大写。我把它改成了getfile。

import java.util.*;
import java.io.*;
public class HangmanProject{
   public static void main(String[] args) throws FileNotFoundException{ //this is the first thing that runs, the main method
      GetFile gf = new GetFile();  //this will create the getFile object
      try {
         gf.getWords(); //runs the method that we make later
      }
      catch(FileNotFoundException potatoes) {   //this will print out error message if there is no words.txt
         System.out.println(potatoes.getMessage());
      }                                
    } //closes main method
}

好 啊;现在进入实际的类本身。

import java.util.*;
import java.io.*;
public class GetFile {        
    //Here are your variables
    private String[] wordList = new String[10];   
    private int x = 0;                             
    //good practice to have variables private;
    //Now it is time for the constructor.
    public GetFile() 
       { 
     //empty constructor; you don't need to have any instance variables declared.
       } 
    public void getWords() throws FileNotFoundException {  //the big method for doing stuff
        Scanner keyboard = new Scanner(System.in);    // to read user's input
        System.out.println("Welcome to Hangman Project!");
        Scanner wordScan = null;
        wordScan = new Scanner(new File("words.txt"));
        while (wordScan.hasNext()) { //checking if there are more words
            wordList[x] = wordScan.next();  
            System.out.println(wordList[x]);  //print them out as the array is filled
            x++;  
            }
        if (wordScan != null)
           {
           wordScan.close(); //close the file after you are finished
           }
    }
}
vm0i2vca

vm0i2vca4#

构造函数是在java和其他oo语言中初始化对象的特殊子例程。如果不声明构造函数,类将有一个隐式默认构造函数(不执行任何操作并返回)。
您的问题的注解中提到的代码的其他问题是它根本无法编译。不能将代码放在不是字段或类声明的类主体中。不是类或字段声明的代码必须存在于构造函数、方法、静态方法、初始值设定项或静态初始值设定项中。
oracle自己的java教程是开始学习java语言的极好资源。这些教程可以在oracles网站上找到,第一个教程(以下链接为trail:getting start)将帮助您编写和理解第一个java程序。对于手头的特定问题,解释类和对象的使用的教程(也链接到下面)将很有用。
一旦你走得更远,你可能会发现,你可以问一些更具体的问题,关于你卡住的概念。
trail:入门
课程:类和对象
java教程

qeeaahzv

qeeaahzv5#

下面是一个例子,我试图解决大多数关于如何使用java的问题:-)在将来特别考虑使用像eclipse这样的ide进行编程http://www.eclipse.org/downloads/ 在尝试更复杂的事情之前先学习java的基础知识。
首先,将“helloworld”打印到控制台是一种方式,然后逐件进行,创建另一个类(可能是两个),创建它们的一些示例,并使它们也将消息打印到控制台。不要急于求成!学习基础知识是你将来遇到的每一门语言的必修课,祝你旅途好运!:)
主.java

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

          System.out.println("Welcome to Hangman Project!");

          // lets initialize your FileHandler class, this will call its constructor btw.
          FileHandler fileHandler = new FileHandler();

          // and lets call its method after that
          fileHandler.readFile();

       }     

    }

文件处理程序.java

public class FileHandler{

   // place for your attributes belonging to full class

   // Im the constructor
   public FileHandler() {
         // you can do something here like your initializations for attributes
         System.out.println("FileHandler instance created!");
   }

   public void readFile() {
         // do your file reading here
         System.out.println("FileHandler readfile() called!");
   }

}

相关问题