Klass orqali avtomobil atributlari va xususiyatlarini yaratish


Download 13.9 Kb.
Sana09.01.2022
Hajmi13.9 Kb.
#264287
Bog'liq
Java Class, Objects, atribut


// Class qismi

// klass orqali avtomobil atributlari va xususiyatlarini yaratish


package com.company;
public class Car {
String model;
String color; // rangi
String number; // Davlat raqami
int year;
int maxSpeed; //tezligi

double km; // moshina umumiy bosib o`tgan yo`li


boolean state;

void startOn(){


state = true;
System.out.println("Mashina o`t oldirildi");
}
void startOff(){
state = false;
System.out.println("Mashina o`chirildi");
}
void drive(double distance) // bu yerda (double distanse) yangi xususiyat orqali e`lon qilish
{
km += distance; //masofani km da yozib boradi
System.out.println("Mashina" + distance + " masofani bosib o`tgan");
}

}

// Main qismi


package com.company;

public class Main {


public static void main(String[] args) {
// Car kalssidan 1-obyektni(car1) yaratish new orqali
Car car1 = new Car();
car1.km = 100;
// car1.state = false; ->>> bu yerda state qiymati default qabul qilinadi car classidagi
car1.color = "Qora";
car1.model = "Tracker";
car1.maxSpeed = 220;
car1.number = "01 A777AA";
car1.year = 2020;

// Car kalssidan 2-obyektni(car2) yaratish new orqali


Car car2 = new Car();
car2.km = 1500;
car1.color = "Asfalt rang";
car1.model = "Malibu";
car1.maxSpeed = 240;
car1.number = "10 B888CD";
car1.year = 2019;
}
}
Download 13.9 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling