如何检查参数是否存在?
所以我有一个视频库存程序,
用户通过初始化构造函数来创建视频类的对象
Video video=new Video(String name){
this.name=name
}
现在我有三个以上的参数化构造函数
Video video=new Video(String name, int rating){
this.name=name;
this.rating=rating;
}
Video video=new Video(String name, int rating, boolean checkout){
this.name=name;
this.rating=rating;
this.checkout=checkout;
}
Video video=new Video(String name, boolean checkout){
this.name=name;
this.checkout=checkout;
}
添加的每个视频都存储在 Video[] store
数组
中用户仅通过传递名称(例如Matrix)来添加视频并创建视频对象,它会添加到Video[]存储
中
我想要的是每当另一个参数添加到 Matrix 时,假设int rating
,它首先从Video[] store
数组中找到Matrix,然后检查是否boolean checkout
参数存在或不存在。 如果 boolean checkout
存在,它将值传递给构造函数 Video video=new Video(String name, int rating, boolean checkout)
否则它将值传递给构造函数Video video=new Video(String name, int rating)
。
我想知道如何检查参数是否存在。
我想要采取的另一种方法是设置一个构造函数 Video(String name)
并使用 null 值来初始化变量,然后使用 setters 更改它们,但你不能将 int
和 boolean
设置为 null。 所以我必须像这样使用它(正如我在互联网上找到的那样),
Video(String name){
this.videoName=name;
this.rating=(Integer) null;
this.checkout=(Boolean) null;
}
但这需要拆箱或我不知道该怎么做。因此,任何帮助将不胜感激。 :)
So I have a video inventory program,
Where the user creates a object of the class video by initializing the constructor
Video video=new Video(String name){
this.name=name
}
Now I have three more parameterized constructor
Video video=new Video(String name, int rating){
this.name=name;
this.rating=rating;
}
Video video=new Video(String name, int rating, boolean checkout){
this.name=name;
this.rating=rating;
this.checkout=checkout;
}
Video video=new Video(String name, boolean checkout){
this.name=name;
this.checkout=checkout;
}
Each video added is stored in a Video[] store
array
First the user adds the video only by passing the name (eg.Matrix) and creates the object of the video, it gets added in the Video[] store
What I want is that whenever a another parameter is added to Matrix , suppose int rating
, it first finds Matrix from the Video[] store
array, then checks whether the boolean checkout
parameter exists or not.
if boolean checkout
exists it passes the values to the constructor Video video=new Video(String name, int rating, boolean checkout)
else it passes values to the constructor Video video=new Video(String name, int rating)
.
I want to know how to check if a parameter exists or not.
Another approach I wanted to take was to set one constructor Video(String name)
and use null values to initialize the variables and later change them using setters, but you can't set int
and boolean
to null.
So I have to use it like this (as I found on the internet)
Video(String name){
this.videoName=name;
this.rating=(Integer) null;
this.checkout=(Boolean) null;
}
but this requires unboxing or something which I have no idea how to do. So any help would be appreciated. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论