By GokiSoft.com|
21:04 18/03/2020|
Java Basic
Share Code- Lession 4 - Java Basic - C1907L
Overview buổi trước
- Hướng dẫn lại chủ đề nào
=> Getter/setter
=> Kiem tra dieu kien du lieu (su ly tap chung)
=> Getter/Setter => no se cho phep cac ban goi toi thuoc tinh cua class object
=> Trong nguyen tac lap trinh: Khong nen cai dat du lieu truc tiep len thuoc => su dung getter/setter
- Chữa bài tập
Kiến thức mới
=> Tính chất lập trình hướng đối tượng
=> Tính bao đóng : buổi trước
=> Tính kế thừa : buổi trước
=> Tinh đa hình : Today
=> Tính trừu tượng : Today
=> Giai thich vua roi => tinh truu tuong
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lession4;
/**
*
* @author Diep.Tran
*/
public abstract class Animal {
String name, foodType;
public Animal() {
}
public Animal(String name, String foodType) {
this.name = name;
this.foodType = foodType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFoodType() {
return foodType;
}
public void setFoodType(String foodType) {
this.foodType = foodType;
}
public void running() {
System.out.println("Animal is running");
}
public void testing() {
System.out.println("Testing....");
}
//so sanh diem loi => phuong thuc truu tuong
public abstract void showSound();
// //phuong thuc rong.
// public void showSound() {
// //khi de cap toi Dong noi chung
// //Dong se co am thanh
// //Ko biet dc am thanh cua dong vat do la gi
// //rong => ko chua code
// }
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lession4;
/**
*
* @author Diep.Tran
*/
public class Cat extends Animal{
String color;
public Cat() {
}
public Cat(String color, String name, String foodType) {
super(name, foodType);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void running() {
super.running();
System.out.println("Cat is running");
// testing();
super.testing();
}
@Override
public void showSound() {
System.out.println("Meo meo ...");
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lession4;
/**
*
* @author Diep.Tran
*/
public class Dog extends Animal{
@Override
public void running() {
System.out.println("Dog is running");
}
@Override
public void showSound() {
System.out.println("Go .. go ..");
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lession4;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
Student std = new Student();
std.fullname = "Tran Van A";
// std.age = -12;
std.setAge(-12);
//age >= 0 && age <= 200 => tai su duoc.
System.out.println("Fullname : " + std.fullname);
System.out.println("Age : " + std.age);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lession4;
/**
*
* @author Diep.Tran
*/
public class Student {
String fullname;//public, private, protected => protected
int age;//age >= 0 & age <= 200
public Student() {
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age >=0 && age <= 200) {
this.age = age;
} else {
System.err.println("Khong dc cai dat tuoi < 0 hoac > 200");
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lession4;
import java.util.ArrayList;
/**
*
* @author Diep.Tran
*/
public class Test {
public static void main(String[] args) {
//dang dung roi.
// Animal animal = new Animal();
// animal.running();
Cat cat = new Cat();
cat.running();
cat.showSound();
//lay vi du khac
//Chu y : khai bao kieu du lieu la lop cha => khoi tao doi tuong >> khoi tao tu class parent hoac class child
Animal abc = new Cat();
//abc => no la doi tuong gi.
//abc => no la doi tuong Cat
abc.running();
//dac tinh nay => dac tinh da hinh
//compile => Animal
//runtime => luc chuong trinh dang chay => Cat
//Xet vai tro cua cac : ban be, thay - tro, con - cha, chau - ong, cha - con
//van chinh la cac ban
//Cat bbb = new Animal();//sai => khong khoi tao duoc
//vay lam the nao chung ta co the su dung => goi dc cac phuong thuc va thuoc tinh
//trong class Cat cua doi tuong abc
//dc su dung trong TH => bai toan da hinh
if(abc instanceof Cat) {
((Cat) abc).showSound();
}
//tai sao chung ta su dung toi da hinh trong lap trinh
//viet chuong trinh quan am thanh cua dong
ArrayList<Cat> catList = new ArrayList<>();
//2 dong code nay
Cat cat2 = new Cat();
cat2.running();
catList.add(cat2);
//viet gom lai thanh 1 nhu sau
catList.add(new Cat());
ArrayList<Dog> dogList = new ArrayList<>();
dogList.add(new Dog());
dogList.add(new Dog());
dogList.add(new Dog());
dogList.add(new Dog());
for (Dog dog : dogList) {
dog.showSound();
}
for (Cat c : catList) {
c.showSound();
}
//Chi muon dung ArrayList => quan ly tat ca cac dong vat (Cat, Dog, Tiger, Bear, Bee, ...)
ArrayList<Animal> animalList = new ArrayList<>();
animalList.add(new Cat());
animalList.add(new Cat());
animalList.add(new Dog());
animalList.add(new Dog());
animalList.add(new Cat());
animalList.add(new Dog());
animalList.add(new Dog());
animalList.add(new Dog());
for (Animal animal1 : animalList) {
// animal1.showSound();
animal1.running();
if(animal1 instanceof Cat) {
((Cat) animal1).showSound();
} else if(animal1 instanceof Dog) {
((Dog) animal1).showSound();
}
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)