Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lectures / Lecture1Optional

.java
Скачиваний:
26
Добавлен:
14.10.2016
Размер:
1.83 Кб
Скачать
import java.util.Scanner;


// class - abstract data type
// object - instance of the class

// access modifiers: public, private, protected
class Human {

// variable
// local
// class

// methods
//constructor



// instance variables
private int age;
private String name;

static int number;

//default constructor
public Human(){}
public Human(){
this.age = 0;
this.name = "";
}
// parametrized constructor
public Human(int age){
this.age = age;
}
// fully-parametrized constructor
public Human(int age, String name){
this.age = age;
this.name = name;
}

// create getter/setter methods for each instance variable
// getter
public int getAge() {
return age;
}
// setter
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// you may add any methods inside the class
public void sayHello(){
System.out.println("Hello");
}

// main method
public static void main(String args[]){

int x; // statement
int v; // declaration
int y = 10; // initialization

Human human;//reference
Human human1 = new Human(); // object
Human human2 = new Human(22,"Ed");
Human human3 = new Human(24,"Ann");

// streams in java
// cout = System.out
// cin = System.in

Scanner sc = new Scanner(System.in);
System.out.println("Enter name, then age");
String name = sc.nextLine();
int age = sc.nextInt();

// we can get acces to the private variables only
// through the public methods
human1.name = name;
human1.setAge(age);
//human1.age = 10; - error, because age is private

human3 = human2;

int x = 4;
int y = x;
x = 5;

}

}
Соседние файлы в папке Lectures