尝试创建对象的新实例。我想制作一部新电影并将其展示在一个arraylist中。
所以我有两节课。
第1类
public class Films {
//Film Attributes
private String title, studio, director;
private int release, duration, rating;
//default constructor
public Films(){
title = null;
studio = null;
director = null;
release = 0;
duration = 0;
rating = 0;
}
//Start of Film method
public void setFilms(String title, String studio, String director,
int release, int duration, int rating)
{
this.title = title;
this.studio = studio;
this.director = director;
this.release = release;
this. duration = duration;
this.rating = rating;
}//end of Film method
//Start of getString method
public String getString(){
return "\nTitle: " + title + "\nRelease: " + release + "\nDuration: " + duration
+ "\nStudio: " + studio + "\nDirector: " + director + "\nRating: " + rating
+ "\n";
}//end of getString method
}
和2级
//start of createFilm() method
private static void createFilm(){
Films newfilm = new Films(); //create a new instance of a film object
System.out.println("Film Title: ");
String title = in.next();
System.out.println("Release Date: ");
int release = in.nextInt();
System.out.println("Duration: ");
int duration = in.nextInt();
System.out.println("Studio: ");
String studio = in.next();
System.out.println("Director: ");
String director = in.next();
System.out.println("Rating: ");
int rating = in.nextInt();
newfilm.setFilms(title, release, duration, studio, director, rating);
myfilms.add(newfilm);
}
}
我只包括了我在课堂上遇到麻烦的方法。基本上我遇到的麻烦就是在第2课那一行;
Films newfilm = new Films(); //create a new instance of a film object
告诉我'电影无法解析为类型' X2。所以两部电影都是'。
我想象一些非常愚蠢和愚蠢的东西,我忽略了,假设或错过了但是我无法弄清楚问题是什么。
这是我关于这个主题的第一篇文章,因为如果我错过了有价值的关键信息和部分代码,你可以告诉我很抱歉。我是编程新手。
答案 0 :(得分:0)
这是一个导入问题,你需要在这里添加导入语句是一个例子
注意第一行包com.ura.stam.com.ura.stammm;它基本上是你班级的地方
package com.ura.stam.com.ura.stammm;
public class MyFilms {
public MyFilms(){
System.out.println("Callin constructor of films class");
}
}
现在看看我如何将它导入其他类 注意第二行导入com.ura.stam.com.ura.stammm.MyFilms;它告诉编译将此类带到MyTestClass
package com.ura.stam.com.ura.sdss;
import com.ura.stam.com.ura.stammm.MyFilms;
public class MyTestClass{
public static void main(String[] args) {
MyFilms myFilms = new MyFilms();
}
}