这是我关于 setter 和 getter 的任务,由于某种原因它不起作用。谁能检查一下我的问题是什么?谢谢你。
public class FlightTest
{
public static void main (String [] args)
{
String name;
String number;
String Orig;
String Desti;
Scanner scan = new Scanner (System.in);
Flight data = new Flight ();
System.out.print ("Airline Name: ");
String AirlineName = scan.nextLine ();
data.setAirlineName (name);
System.out.print ("Flight Number: ");
String FlightNumber = scan.nextLine ();
data.setFlightNumber (number);
System.out.print ("Origin: ");
String Origin = scan.nextLine();
data.setOrigin (Orig);
System.out.print ("Destination: ");
String Destination = scan.nextLine ();
data.setDestination (Desti);
System.out.println (data); …Run Code Online (Sandbox Code Playgroud) 这是一种奇怪的行为,我不明白我有。
我有一个带有列表的类 a 和一个吸气剂:
class A
{
private:
std::list<OtherClass *> l;
public:
std::list<OtherClass *> getL()
{
return l;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,如果我做类似的事情:
A inst;
std::list<OtherClass *>::iterator itB = inst.getL().begin();
std::list<OtherClass *>::iterator itE = inst.getL().end();
for (; itB != itE; ++itB) // Instant ABORT !
Run Code Online (Sandbox Code Playgroud)
但如果我这样做:
A inst;
std::list<OtherClass *> l = inst.getL();
std::list<OtherClass *>::iterator itB = l.begin();
std::list<OtherClass *>::iterator itE = l.end();
for (; itB != itE; ++itB) // It works now !
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么会发生这种情况吗?为什么我必须通过这样的临时变量才能不中止?先感谢您 !
我在使用定义为类型同义词的元组的情况下,所以我不能真正使用记录结构.有没有办法获得这些元素?
这里更具体的是我的意思:
type Movie = (Title, Regisseur, MainActors, ReleaseDate, Genre, SalesPrice)
type Title = String
type SalesPrice = Int
etc
Run Code Online (Sandbox Code Playgroud)
我怎样才能只Title和ReleaseDate一个例子.除了为元组中的每个位置定义一个getter函数.
我想把一个空的输入/字符串变成null,但似乎我无法让它工作。下面是处理 $_POST 的类,
class post
{
public $data = array();
public function get($param, $default = null) {
if (!isset($this->data[$param])) {//not set, return default
return $default;
}
else if(isset($this->data[$param]) && $default === ''){//empty string
return null;
}
return $this->data[$param];
}
}
Run Code Online (Sandbox Code Playgroud)
例如,
$post = new post();
$test = $post->get('url','');
var_dump($test);
Run Code Online (Sandbox Code Playgroud)
我明白了,
string '' (length=0)
Run Code Online (Sandbox Code Playgroud)
但我想要null。是否可以?
所以我有 :
ArrayList<data> mi = new ArrayList<data>();
Run Code Online (Sandbox Code Playgroud)
我有一个查询,其中包括:
while (rs.next()) {
mi.add(new users());
mi.get(i).name= rs.getString("name");
mi.get(i).pass= rs.getString("pass");
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理,但我希望它与Getter/setters一样:
mi.get(i).setname( rs.getString("name"));
Run Code Online (Sandbox Code Playgroud)
编辑:拼错我知道了
为什么我不能称之为方法?
第一次海报,长期潜伏者.
我是第二年计算机科学专业学生,试图编写一个二十一点游戏.这不是一个家庭作业,它只是一个有趣的项目.它没有这样做,所以游戏本身还没准备好,所以请忽略该部分.我花了大约两个小时搜索SO以获得答案,但无法将其他用户的类似问题与我自己的问题进行比较,因此我决定发布我的问题.
我的问题是'deck'ArrayList.从我的Blackjack.java文件中,无论我做什么,我都无法调用它,我无法调用我的Card类中的任何方法,我无法操纵它,我似乎无法做任何事情而不会收到某种错误.
public class Blackjack {
public int bet;
public int earnings = 0;
public int total = 100;
public boolean inputNeeded = true;
public Blackjack() {
Scanner s = new Scanner(System.in);
System.out.println(" Welcome to Blackjack.");
System.out.println(" Enter any key to continue.");
System.out.println(" Otherwise, please type Exit to quit.");
while (!s.nextLine().toString().toLowerCase().equals("exit")) //Main Game Loop - Only ends when user types Exit.
{
while (inputNeeded) {
System.out.println("You currently have $" + total + ".");
System.out.print("Please place your bets now. (Min …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
var obj={
prop1:7,
get prop2 (){
console.log('I ran');
return {a:1,b:2}
}
}
Run Code Online (Sandbox Code Playgroud)
什么时候{a:1,b:2} 创建对象?
当上面的代码运行或我们明确运行时obj.prop2?我遇到了这个问题,因为通过写obj.prop2.a我们得到1,我无法解释这种行为.如果不运行obj.prop2怎么知道它的价值obj.prop2.a?
而且,在我们刚写的时候在控制台中obj.prop2.建议自动完成,在另一个中,a和b.怎么样,因为我们没有跑obj.prop2?
我目前正在尝试Class.getMethod用作吸气剂。
我尝试使用这种方法:
//This works fine
Class<?> c = Class.forName("Cat");
//This is not working
Cat cat = c.getMethod("getCat");
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
我是新来的扑腾。我遇到这样的错误,你能帮我吗?
我已经在 http 的 json 中卡住了 5 天,源代码中的代码不起作用。:( L
它说列表未输入,但当我输入时它不接受它。我不知道问题出在哪一行,但我收到了类似的警告。“构建 FutureBuilder 时抛出以下 NoSuchMethodError(脏,状态:_FutureBuilderState#447cc):”
// To parse this JSON data, do
//
// final post = postFromJson(jsonString);
import 'dart:convert';
Post postFromJson(String str) => Post.fromJson(json.decode(str));
String postToJson(Post data) => json.encode(data.toJson());
class Post {
Post({
required this.userId,
required this.id,
required this.title,
required this.body,
});
int userId;
int id;
String title;
String body;
factory Post.fromJson(Map<String, dynamic> json) => Post(
userId: json["userId"],
id: json["id"],
title: json["title"],
body: json["body"],
);
Map<String, dynamic> toJson() => …Run Code Online (Sandbox Code Playgroud) 我有一个具有以下形式的数据类:
data class ContentElementField(val type: String) {
val text: String? = null
get() = requireNotNull(field)
val style: String? = null
get() = requireNotNull(field)
val path: String? = null
get() = requireNotNull(field)
val caption: String? = null
get() = requireNotNull(field)
}
Run Code Online (Sandbox Code Playgroud)
当我想要执行以下操作时出现问题:
when (it.type) {
"text" -> TextElement(Text(it.text), Style(it.style))
"image" -> ImageElement(Path(it.path), Caption(it.caption))
}
Run Code Online (Sandbox Code Playgroud)
编译器警告我这一点You cannot send a nullable type to a function that does not accept nullable arguments.
即使该字段被标记为可为空,但其 getter 也被标记为不可为空。
编译器应该使用 getters 来决定是否给出这个警告。
您会提供什么来解决这个问题?