Javaで@JsonAutoDetectアノテーションを使用するのはいつですか?
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import java.io.*;
public class JsonAutoDetectTest {
public static void main(String[] args) throws IOException {
Address address = new Address("Madhapur", "Hyderabad", "Telangana");
Name name = new Name("Raja", "Ramesh");
Student student = new Student(address, name, true);
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(student);
System.out.println("JSON: " + jsonString);
}
}
// Address class
class Address {
private String firstLine;
private String secondLine;
private String thirdLine;
public Address(String firstLine, String secondLine, String thirdLine) {
this.firstLine = firstLine;
this.secondLine = secondLine;
this.thirdLine = thirdLine;
}
public String getFirstLine() {
return firstLine;
}
public String getSecondLine() {
return secondLine;
}
public String getThirdLine() {
return thirdLine;
}
}
// Name class
class Name {
private String firstName;
private String secondName;
public Name(String firstName, String secondName) {
this.firstName = firstName;
this.secondName = secondName;
}
public String getFirstName() {
return firstName;
}
public String getSecondName() {
return secondName;
}
}
// Student class
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
class Student {
private Address address;
private Name name;
private Boolean isActive;
public Student(Address address, Name name, Boolean isActive) {
this.address = address;
this.name = name;
this.isActive = isActive;
}
} {
"address" : {
"firstLine" : "Madhapur",
"secondLine" : "Hyderabad",
"thirdLine" : "Telangana"
},
"name" : {
"firstName" : "Raja",
"secondName" : "Ramesh"
},
"isActive" : true
}
-
Javaでpack()メソッドを使用できるのはいつですか?
pack() メソッドはウィンドウで定義されています Javaのクラスであり、すべてのコンテンツが適切なサイズ以上になるようにフレームのサイズを設定します。 pack()の代替 メソッドは、 setSize()を呼び出して、フレームサイズを明示的に確立することです。 またはsetBounds() メソッド。通常、 pack()を使用します setSize()よりもメソッドを呼び出す方が望ましい パックはフレームレイアウトマネージャーにフレームサイズを任せ、レイアウトマネージャーはプラットフォームの依存関係やコンポーネントサイズに影響を与えるその他の要因にうまく適応できるためです。 構文 p
-
Pythonで%sの代わりに%rを使用するのはいつですか?
%s指定子はstr()を使用してオブジェクトを変換し、%rはrepr()を使用してオブジェクトを変換します。 整数などの一部のオブジェクトでは、同じ結果が得られますが、repr()は、(これが可能なタイプの場合)通常は有効なPython構文である結果を返すという点で特別です。これを使用して、オブジェクトを明確に再作成できます。を表します。たとえば、エンドライン文字を含む文字列がある場合、%sは実際には新しい行にデータを表示しますが、%rは出力を\ nとして提供し、引用符もそのまま保持します。 例 >>> string = "Hello\nworld" &