Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Tworzenie obiektów

1. Napisz program, który utworzy obiekt klasy Osoba i przypisze wartości do jej pól
imię, nazwisko, wiek.

class Person
{
public string Name;
public string LastName;
public int Age;
}

2. Utwórz klasę o nazwie prostokąt z polami szerokość i wysokość. Następnie utwórz


obiekt i przypisz wartości do pól.

using System;

public class Rectangle


{
public int Width { get; set; }
public int Height { get; set; }
}

public class Program


{
public static void Main(string[] args)
{
Rectangle rectangle = new Rectangle();
rectangle.Width = 10;
rectangle.Height = 20;
}
}
3. Utwórz klasę o nazwie student z polami nazwa, wiek, i stopień. Następnie utwórz
obiekt klasy student i przypisz wartości do pól.

using System;
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public int Grade { get; set; }
}

public class Program


{
public static void Main(string[] args)
{
Student student = new Student();
student.Name = "Anna Nowak";
student.Age = 18;
student.Grade = 12;
}
}

4. Utwórz klasę o nazwie pracownik z polami nazwa, wypłata i data zatrudnienia.


Następnie utwórz obiekt klasy i przypisz wartości do pól.

using System;
public class Employee
{
public string Name { get; set; }
public double Salary { get; set; }
public DateTime HireDate { get; set; }
}

public class Program


{
public static void Main(string[] args)
{
Employee employee = new Employee();
employee.Name = "Jan Kowalski";
employee.Salary = 5000;
employee.HireDate = new DateTime(2022, 1, 1);
}
}
5. Utwórz klasę o nazwie konto bankowe z polami numer konta, stan i właściciel.
Następnie utwórz obiekt klasy i przypisz wartości do pól.

using System;

public class BankAccount


{
public string AccountNumber { get; set; }
public double Balance { get; set; }
public string Owner { get; set; }
}

public class Program


{
public static void Main(string[] args)
{
BankAccount bankAccount = new BankAccount();
bankAccount.AccountNumber = "1234567890";
bankAccount.Balance = 10000;
bankAccount.Owner = "Jan Kowalski";
}
}
6. Utwórz klasę okrąg z polem promień. Następnie utwórz metody na obliczenie pola
i obwodu koła oraz utwórz obiekt klasy okrąg i przypisz wartość oraz przedstaw
wynik pola i obwodu koła.

using System;

public class Circle


{
public double Radius { get; set; }

public double CalcArea()


{
return Math.PI * Math.Pow(Radius, 2);
}

public double CalcPerimeter()


{
return 2 * Math.PI * Radius;
}
}

public class Program


{
public static void Main()
{
Circle circle = new Circle() { Radius = 4 };
Console.WriteLine(circle.CalcArea());
Console.WriteLine(circle.CalcPerimeter());
}
}
7. Utwórz klasę kraj z polami nazwa oraz nazwa skrótowa. Dodaj metody na zmianę
nazwy i nazwy skróty. Każda zmiana nazwy czy skrótu powinna zwrócić ten sam
obiekt. Pola nazwa kraju i skrót powinny być dostępne tylko do odczytu. Utwórz
metodę statyczną, która będzie tworzyć taki obiekt Create. Każde tworzenie obiektu
powinno odbywać się tylko za pomocą metody Create.

using System;

public class Country


{
public string Name { get; private set; }
public string Code { get; private set; }

private Country(string name, string code)


{
Name = name;
Code = code;
}

public Country ChangeName(string name)


{
Name = name;
return this;
}

public Country ChangeCode(string code)


{
Code = code;
return this;
}

public static Country Create(string name, string


code)
{
return new Country(name, code);
}
}
public class Program
{
public static void Main()
{
Country country = Country.Create("P0land",
"PL123");
Console.WriteLine($"Country {country.Name}
{country.Code}");
country = country.ChangeCode("PL");
Console.WriteLine($"Country {country.Name}
{country.Code}");
country = country.ChangeName("Poland");
Console.WriteLine($"Country {country.Name}
{country.Code}");
}
}

You might also like