尝试将Name
类从Name
类调用到我的Type
类,但我不知道该怎么做。我这样做是因为后来我想为每个对象增加价格,这就是我这样做的原因
public class Name {
public static void main(String [] args) {
NameS hotel = new NameS();
hotel.setName("1. Shangri-La");
NameS hotel1 = new NameS();
hotel1.setName("2. Pullman");
NameS hotel2 = new NameS();
hotel2.setName("3. Royal Chulan");
NameS[]hotellist = new NameS [3];
hotellist[0] = hotel;
hotellist[1] = hotel1;
hotellist[2] = hotel2;
for (int i=0;i<hotellist.length;i++)
{
hotellist[i].print();
}
NameS mall = new NameS();
mall.setName("1. 1Utama");
NameS mall1 = new NameS();
mall1.setName("2. Paradigm");
NameS mall2 = new NameS();
mall2.setName("3. Curve");
NameS[]malllist = new NameS [3];
malllist[0] = mall;
malllist[1] = mall1;
malllist[2] = mall2;
for (int i=0;i<malllist.length;i++)
{
malllist[i].print();
}
NameS restaurant = new NameS();
restaurant.setName("1. Nandos");
NameS restaurant1 = new NameS();
restaurant1.setName("2. Kura");
NameS restaurant2 = new NameS();
restaurant2.setName("3. Dining at Sky");
NameS[]restaurantlist = new NameS [3];
restaurantlist[0] = restaurant;
restaurantlist[1] = restaurant1;
restaurantlist[2] = restaurant2;
for (int i=0;i<restaurantlist.length;i++)
{
restaurantlist[i].print();
}
}
}
Type类
public class Type extends Homepage {
public static void main(String [] args) {
ArrayList<ReviewArrayList> list = new ArrayList <ReviewArrayList>();
String mall;
String restaurant;
Scanner input = new Scanner(System.in);
int choice = 0;
System.out.println("==============================");
System.out.println(" Type of Facilities ");
System.out.println("==============================");
System.out.println("1. Hotel");
System.out.println("2. Shopping Mall");
System.out.println("3. Restaurant");
System.out.println("Choice > ");
choice = input.nextInt();
if(choice == 1) {
System.out.println(hotel.getName());
System.out.println(hotel1.getName());
System.out.println(hotel2.getName());
}
}
}
答案 0 :(得分:0)
说完
private Name name = new Name();
void() {
/*name.objectName; calls the Object objectName that is inside the class Name*/
}
答案 1 :(得分:0)
在main
课程的Name
方法中,您创建Array
hotellist
个NameS
个hotellist
个对象,然后忽略这些对象。
我的建议是制作malllist
,restaurantlist
和type
公共静态类字段。这意味着您的public class Name {
public static NameS[] hotelList = new NameS[3];
public static NameS[] mallList = new NameS[3];
public static NameS[] restaurantList = new NameS[3];
public static void main(String[] args) {
NameS hotel = new NameS();
hotel.setName("1. Shangri-La");
NameS hotel1 = new NameS();
hotel1.setName("2. Pullman");
NameS hotel2 = new NameS();
hotel2.setName("3. Royal Chulan");
hotelList[0] = hotel;
hotelList[1] = hotel1;
hotelList[2] = hotel2;
for (int i=0;i<hotelList.length;i++)
{
hotelList[i].print();
}
NameS mall = new NameS();
mall.setName("1. 1Utama");
NameS mall1 = new NameS();
mall1.setName("2. Paradigm");
NameS mall2 = new NameS();
mall2.setName("3. Curve");
mallList[0] = mall;
mallList[1] = mall1;
mallList[2] = mall2;
for (int i=0;i<mallList.length;i++)
{
mallList[i].print();
}
NameS restaurant = new NameS();
restaurant.setName("1. Nandos");
NameS restaurant1 = new NameS();
restaurant1.setName("2. Kura");
NameS restaurant2 = new NameS();
restaurant2.setName("3. Dining at Sky");
restaurantList[0] = restaurant;
restaurantList[1] = restaurant1;
restaurantList[2] = restaurant2;
for (int i=0;i<restaurantList.length;i++)
{
restaurantList[i].print();
}
课程可以访问它们。例如:
Type
现在,在public static void main(String [] args)
{
ArrayList<ReviewArrayList> list = new ArrayList <ReviewArrayList>();
String mall;
String restaurant;
Scanner input = new Scanner(System.in);
int choice = 0;
System.out.println("==============================");
System.out.println(" Type of Facilities ");
System.out.println("==============================");
System.out.println("1. Hotel");
System.out.println("2. Shopping Mall");
System.out.println("3. Restaurant");
System.out.println("Choice > ");
choice = input.nextInt();
if(choice == 1) {
System.out.println(Name.hotelList[0].getName());
System.out.println(Name.hotelList[1].getName());
System.out.println(Name.hotelList[2].getName());
}
}
课程中,您可以简单地说
test.prototype = {
constructor: test,
myMethod: function() {
this.myArray.push("ok");
}
};
答案 2 :(得分:0)
这是你从当前班级调用一个对象的方法...... 通过制作公共方法,您可以从其他类到达您的字段。 但如果我是你,我只会从一开始就建立一个构造函数来申请名称:)
public class Name
{
private String name;
public void setName(String name)
{
this.name = name;
}
}
public class Main
{
Name myName = new Name();
public static void main(String[] args)
{
myName.setName("Marc");
}
}