我有一个叫做Student的类,当然,这不是全部代码,我只是想了解这个概念。我想在创建了大学类obj的User类中使用另一个obj。大学班是其中包含另一班obj的班。到目前为止,我只做过关联,还没有继承,因此与关联有关的解决方案会有所帮助。
public class College
{
private Student[] student;
private Teacher[] teacher;
int count;
public College()
{
student = new Student[9];
teacher = new Teacher[9];
count = 0;
}
public Student[] getStudent()
{
return student;
}
public void addStudent(Student inStudent)
{
student[count] = new Student(inStudent);
count++;
}
public void setStudent(Student[] student)
{
this.student = student;
}
public Teacher[] getTeacher()
{
return teacher;
}
public void setTeacher(Teacher[] teacher)
{
this.teacher = teacher;
}
public boolean equals(Object inObj)
{
boolean isEqual = true;
College inCollege = (College)inObj;
if(student.length == inCollege.getStudent().length)
{
for(int i = 0; i < student.length; i++)
{
if(!student[i].equals(inCollege.student[i]))
{
isEqual = false;
}
}
}
if((teacher.length == inCollege.getTeacher().length) )
{
isEqual = false;
for(int i = 0; i < inCollege.getTeacher().length; i++ )
{
if(teacher[i].equals(inCollege.teacher[i]))
{ System.out.println("im in");
isEqual = true;
}
}
}
return isEqual;
}
}
问题来源:Stack Overflow
您需要具有public访问器(获取器),private才能在其他类中访问成员。
如下进行:
import java.util.Scanner;
class Engine {
String name;
int year;
String manufacturer;
public Engine() {
}
public Engine(String name, int year, String manufacturer) {
this.name = name;
this.year = year;
this.manufacturer = manufacturer;
}
// getters and setters of instance variables
@Override
public String toString() {
return "Engine [name=" + name + ", year=" + year + ", manufacturer=" + manufacturer + "]";
}
}
class Submarine {
private String id;
private Engine engine;
public Submarine(String id, Engine engine) {
this.id = id;
this.engine = engine;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public Engine getEngine() {
return engine;
}
@Override
public String toString() {
return "Submarine [id=" + id + ", engine=" + engine + "]";
}
}
class ShipStorage {
private Submarine submarine;
private Submarine[] submarines;
public void setSubmarine(Submarine submarine) {
this.submarine = submarine;
}
public Submarine getSubmarine() {
return submarine;
}
public void setSubmarines(Submarine[] submarines) {
this.submarines = submarines;
}
public Submarine[] getSubmarines() {
return submarines;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ShipStorage store = new ShipStorage();
// Input submarines
Submarine[] submarines = new Submarine[3];
String id, name, manufacturer;
int year;
for (int i = 0; i < submarines.length; i++) {
System.out.print("Enter the ID of the sumarine: ");
id = in.nextLine();
System.out.print("Enter the name of its engine: ");
name = in.nextLine();
System.out.print("Enter the manufacturing year of its engine: ");
year = Integer.parseInt(in.nextLine());
System.out.print("Enter the manufacturer's name of its engine: ");
manufacturer = in.nextLine();
submarines[i] = new Submarine(id, new Engine(name, year, manufacturer));
}
// Store submarines to ShipStorage
store.setSubmarines(submarines);
// Display submarines
System.out.println("Displaying the data: ");
for (Submarine submarine : store.getSubmarines()) {
System.out.println(submarine);
}
}
}
运行示例:
Enter the ID of the sumarine: 1
Enter the name of its engine: A
Enter the manufacturing year of its engine: 2010
Enter the manufacturer's name of its engine: X
Enter the ID of the sumarine: 2
Enter the name of its engine: B
Enter the manufacturing year of its engine: 2011
Enter the manufacturer's name of its engine: Y
Enter the ID of the sumarine: 3
Enter the name of its engine: C
Enter the manufacturing year of its engine: 2018
Enter the manufacturer's name of its engine: Z
Displaying the data:
Submarine [id=1, engine=Engine [name=A, year=2010, manufacturer=X]]
Submarine [id=2, engine=Engine [name=B, year=2011, manufacturer=Y]]
Submarine [id=3, engine=Engine [name=C, year=2018, manufacturer=Z]]
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。