所以我不会说我是java新手,但是自从我上次编码以来,我已经忘记了很多。因此,我想知道是否可以将构造函数的参数从一个类发送到另一个类。我假设我必须在“A类”中从“B类”中创建构造函数的对象。但是,我的第二个类中有多个构造函数,除非我能弄清楚如何发送参数,否则我会遇到一个我不想使用的构造函数。这是我的代码片段。
public class Title{
/* Title packet */
private Class<?> packetTitle;
/* Title packet actions ENUM */
private Class<?> packetActions;
/* Chat serializer */
private Class<?> nmsChatSerializer;
private Class<?> chatBaseComponent;
/* Title text and color */
private String title = "Falling Maze";
private ChatColor titleColor = ChatColor.GREEN;
/* Subtitle text and color */
private String subtitle = "Tip: Fall through the maze to win! You may only touch sandstone!";
private ChatColor subtitleColor = ChatColor.RED;
/* Title timings */
private int fadeInTime = 10;
private int stayTime = 20;
private int fadeOutTime = 20;
private boolean ticks = true;
private static final Map<Class<?>, Class<?>> CORRESPONDING_TYPES = new HashMap<>();
/**
* Create a new 1.8 title
*
* @param title
* Title
*/
public Title(String title) {
this.title = title;
loadClasses();
}
/**
* Create a new blank title.
*/
public Title() {
this("");
}
/**
* Create a new 1.8 title
*
* @param title
* Title text
* @param subtitle
* Subtitle text
*/
public Title(String title, String subtitle) {
this.title = title;
this.subtitle = subtitle;
loadClasses();
}
/**
* Copy 1.8 title
*
* @param title
* Title
*/
public Title(Title title) {
// Copy title
this.title = title.title;
subtitle = title.subtitle;
titleColor = title.titleColor;
subtitleColor = title.subtitleColor;
fadeInTime = title.fadeInTime;
fadeOutTime = title.fadeOutTime;
stayTime = title.stayTime;
ticks = title.ticks;
loadClasses();
}
/**
* Create a new 1.8 title
*
* @param title
* Title text
* @param subtitle
* Subtitle text
* @param fadeInTime
* Fade in time
* @param stayTime
* Stay on screen time
* @param fadeOutTime
* Fade out time
*/
public Title(String title, String subtitle, int fadeInTime, int stayTime, int fadeOutTime) {
this.title = title;
this.subtitle = subtitle;
this.fadeInTime = fadeInTime;
this.stayTime = stayTime;
this.fadeOutTime = fadeOutTime;
loadClasses();
}
这是我的类,其中包含用于不同类型标题的所有不同构造函数。我想要做的是能够创建一个标题,副标题,fadeInTime,stayTime和fadeOutTime的标题,并在下面的其他类中调用它。
public class Main extends JavaPlugin implements Listener{
private Title t1 = new Title();
public void onEnable()
{
Bukkit.getPluginManager().registerEvents(this, this);
}
@EventHandler
public void playerJoin(PlayerJoinEvent event)
{
//getting the player's name
Player player = event.getPlayer();
//adding the gold ingot and the bed to their respective inventory slots.
ItemStack bed = new ItemStack(Material.BED);
ItemStack goldIngot = new ItemStack(Material.GOLD_INGOT);
player.getInventory().setItem(0, goldIngot);
player.getInventory().setItem(8, bed);
t1.send(player);
}
当我创建我的Title类的对象时,所有它所做的就是带来我的第一个构造函数(创建一个空白标题的构造函数)。我想要它带来的是能够创建一个完整的标题,就像我之前说的,标题,副标题,fadeInTime,stayTime和fadeOutTime。任何帮助都将非常感激,因为当我尝试将参数传递给我的新对象Title()时,它表示参数无法解析为变量。请提前告诉您。我试图尽可能具体,但问我任何有用的东西,我会尽我所能回答。
安德烈亚斯帮助我解决问题的重要道具。事实证明我的标题无论如何都被覆盖了,所以我在我的Main类中设置了一个for循环,它会发送一个计时器作为我的标题。这使我能够从另一个班级拉出来。非常感谢! ^ - ^有没有办法对你的评论进行投票?
答案 0 :(得分:0)
我很不确定你要求的是什么,但是..如果你需要将输入参数的值从第一个构造函数传递给第二个类构造函数,你可以简单地完成它。 检查下面的伪代码:
让你有2个类(让我说ClassA
和ClassB
),它们在构造函数中都有1个字符串参数,第二个类也有一个整数输入参数,所以它会是这样的:
class ClassA{
/*that will be variable of each instance of that class
* when public, can call something like
* classAinstance.classInstanceVariableA, otherwise you have to make getter for reading and setter for setting
*/
String classInstanceVariableA;
//that is the constructor
ClassA(String paramStr){
//..whatever, eg.
this.classInstanceVariableA = paramStr;
}
//getter example
public String getClassInstanceVariableA(){
return classInstanceVariableA;
}
//setter example
public void setClassInstanceVariableA(String value){
this.classInstanceVariableA = value;
}
}
class ClassB{
ClassA classBinstanceVariableOfClassAType;
int classBintVarInstance;
ClassB(String classBparamConstructor, int param2intConstructor){
//there you can call construtor from the ClassA eg.- that you mean?!?
this.classBinstanceVariableOfClassAType = new ClassA(classBparamConstructor);
this.classBintVarInstance = param2intConstructor;
//when public parameter classInstanceVariableA
//read value this.classBintVarInstance.classInstanceVariableA
//set value this.classBintVarInstance.classInstanceVariableA = "foo"
//when not public and you have getter and setter
//read value this.classBintVarInstance.getClassInstanceVariableA();
//set value this.classBintVarInstance.setClassInstanceVariableA("foo");
}
}
请注意,您无法调用类似classBinstance.classBinstanceVariableOfClassAType
之类的内容,直到它公开或您再次拥有getter ,您还可以创建一个方法从{{返回提到的字符串变量1}}在classA
中,清楚吗?