classGeeks{publicstringauthor_name{get;set;}publicintauthor_id{get;set;}publicinttotal_article{get;set;}}classGFG{ // Main methodstaticpublicvoidMain(){ // Initialize fields using // an object initializerGeeksobj=newGeeks(){author_name="Ankita Saini",author_id=102,total_article=178};Console.WriteLine("Author Name: {0}",obj.author_name);Console.WriteLine("Author Id: {0}",obj.author_id);Console.WriteLine("Total no of articles: {0}",obj.total_article);}}
Output
คำอธิบายตัวอย่าง : จากทั้งตัวอย่าง 1.1 , 1.2 และตัวอย่าง 1.3. จะเห็นได้ว่า ทั้ง Class Student Class Geeks และ Class Cat ได้ สร้าง object และกำหนดค่าเริ่มต้นในเวลาเดียวกันโดยใช้เครื่องหมายวงเล็บปีกกาใน main() โดยไม่ใช้ constructor
public class Cat
{
// Auto-implemented properties.
public int Age { get; set; }
public string? Name { get; set; }
public Cat()
{
}
public Cat(string name)
{
this.Name = name;
}
}
Cat cat = new Cat { Age = 10, Name = "Fluffy" };
Cat sameCat = new Cat("Fluffy"){ Age = 10 };
ตัวอย่างที่ 1.2
Author Name: Ankita Saini
Author Id: 102
Total no of articles: 178
List<Cat> cats = new List<Cat>
{
new Cat{ Name = "Sylvester", Age=8 },
new Cat{ Name = "Whiskers", Age=2 },
new Cat{ Name = "Sasha", Age=14 }
};
var numbers = new Dictionary<int, string>
{
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};
var moreNumbers = new Dictionary<int, string>
{
{19, "nineteen" },
{23, "twenty-three" },
{42, "forty-two" }
}
Setter Methods
public class Person {
private String name; // private = restricted access
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}
Instance Initializer Block
class Bike7{
int speed;
Bike7(){System.out.println("speed is "+speed);}
{speed=100;}
public static void main(String args[]){
Bike7 b1=new Bike7();
Bike7 b2=new Bike7();
}
}
ตัวอย่างที่ 3.3
package com.tutorialspoint;
import java.util.Arrays;
import java.util.List;
public class ArrayDemo {
public static void main (String args[]) {
// create an array of strings
String a[] = new String[]{"abc","klm","xyz","pqr"};
List<String> list = Arrays.asList(a);
// printing the list
System.out.println("The list is:" + list);
}
}
ตัวอย่างที่ 4.1
class Car:
def set_values(self, make, model, year):
self.make = make
self.model = model
self.year = year
my_car = Car()
my_car.set_values('Toyota', 'Camry', 2022)
print(my_car.make) # Output: Toyota
ตัวอย่างที่ 4.2
number = [1, 2, 3, 4, 5]
name = ['Amit', 'kabir', 'bhaskar', 2]
print(number)
print(name)
[1, 2, 3, 4, 5]
['Amit', 'kabir', 'bhaskar', 2]
ตัวอย่างที่ 5.1
#include <iostream>
struct Person {
std::string name;
int age;
std::string address;
};
int main()
{
// Initialize a Person struct using aggregate
// initialization
Person p1{ "John", 33, "New York" };
// Print out the values of the Person struct
std::cout << "Name: " << p1.name << std::endl;
std::cout << "Age: " << p1.age << std::endl;
std::cout << "Address: " << p1.address << std::endl;
return 0;
}
ตัวอย่าง 5.2
#include<iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point(int i = 0, int j = 0):x(i), y(j) {}
/* The above use of Initializer list is optional as the
constructor can also be written as:
Point(int i = 0, int j = 0) {
x = i;
y = j;
}
*/
int getX() const {return x;}
int getY() const {return y;}
};
int main() {
Point t1(11, 17);
cout<<"x = "<<t1.getX()<<", ";
cout<<"y = "<<t1.getY();
return 0;
}