2010-06-21 100 views

回答

2
+0

我想要做的是在腳本中創建一個類(在我的腳本文件中,並使用它像一個Java類),這就是我所需要的。我不想從我的項目中導入一個類,但我需要在腳本中創建它。怎麼做 ??? – 2010-06-22 10:50:14

+0

您可以做的最好的事情就是創建一個Javascript對象,將其轉換爲可調用的對象,然後從中執行此操作。然而,除非你做某種插件系統,否則你會違反一些OOP規則 – TheLQ 2010-06-22 22:18:24

1
import java.io.*; 
public class Employee{ 
String name; 
int age; 
String designation; 
double salary; 

public Employee (String name){ 
    this.name=name; 
} 
public void empAge(int empAge){ 
    age=empAge; 
} 
public void empDesignation(String empDesig){ 
    designation=empDesig; 
} 
public void empSalary(double empSalary){ 
    salary=empSalary; 
    } 
public void printEmployee(){ 
    System.out.println("Name:   "+name); 
    System.out.println("Age:   "+age); 
    System.out.println("Designation: "+designation); 
    System.out.println("Salary:  "+salary); 

} 
} 
1
public class WesternTown { 

    int stables; 
    int saloon; 
    int yearEstablished; 
    int troublemaker; 
    String sheriffsname; 
    String location; 


    public WesternTown() { 
     stables=3; 
     location="Wesrtern America"; 
     yearEstablished=1850; 
    } 

    public WesternTown(String name) { 
     sheriffsname = name; 
    } 


    public static void main (String [] args){ 

     WesternTown newtown = new WesternTown(); 
     WesternTown newname = new WesternTown("SHERIFFER"); 
     System.out.println("stables:  " + newtown.stables); 
     System.out.println("saloon:  " + newtown.saloon); 
     System.out.println("sheriffs:  " + newtown.sheriffsname); 
     System.out.println("troublemaker: " + newtown.troublemaker); 
     System.out.println("location:  " + newtown.location); 
     System.out.println("yearEstablished: " + newtown.yearEstablished); 
     System.out.println("The Name is:  " + newname.sheriffsname); 
    } 
} 
相關問題