2月15日 Core Java 周四

本文通过具体的Java代码示例,展示了面向对象编程的概念和技术,包括继承、多态等特性,并通过交通工具和人员类的具体实现进行说明。

/* hao
* 2018-2-15 08:36:29
* 交通工具
*/
public class Transport {
String type;
String color;
int price;

void start(){
    System.out.println("开始方法");
}
void stop(){
    System.out.println("停止方法");
}

}

public class Bus extends Transport {
int num;
String line;

void start(){
    System.out.println("公交车出站了");
}
void stop(){
    System.out.println("公交车到站了");
}

}

public class Freight extends Transport {
int weight;
int longness;

// Transport(int weight,int longness){
// this.weight=weight;
// this.longness=longness;
// }

void start(){
    System.out.println("火车载着"+weight+"吨东西出发了");
}
void stop(){
    System.out.println("火车行驶了"+longness+"里程之后到达了目的地开始卸货");
}

}
/* hao
* 2018-2-15 08:46:47
* 测试
*/
public class TestTransport {

public static void main(String[] args) {
    Bus bu = new Bus();

    bu.num = 10;
    bu.line= "ee";
    bu.start();
    bu.stop();

    Freight fr = new Freight();
    fr.weight=30;
    fr.longness=40;
    fr.start();
    fr.stop();

}

}
/* hao
* 2018-2-15 09:02:20
* 类
*/
public class Animal2 {
private int id = 1001;
private String name;
private String color;
private int age;

Animal2(){

}

// Animal2(int id){
// this.id = 1001;
// }
public int getId() {
return id;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getColor() {
    return color;
}
public void setColor(String color) {
    this.color = color;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}

void printlnfo(){
    System.out.println("输出");
}
void eat(){
    System.out.println("爱吃");
}

}

public class Dog2 extends Animal2 {

void printlnfo(){
    System.out.println("Dog2 getId()=" + getId() + ", getName()=" + getName() + ", getColor()=" + getColor() + ", getAge()="
            + getAge());
}
void eat(){
    System.out.println("狗吃");
}

}

public class Cat2 extends Animal2 {
void printlnfo(){
System.out.println(“Cat2 [getId()=” + getId() + “, getName()=” + getName() + “, getColor()=” + getColor() + “, getAge()=”
+ getAge());
}
void eat(){
System.out.println(“猫吃”);
}

}

public class Zoo {

public static void main(String[] args) {
    Dog2 dog = new Dog2();

    System.out.println(dog.getId());
    dog.setName("狗");
    System.out.println(dog.getName());
    dog.setColor("黄");
    System.out.println(dog.getColor());
    dog.setAge(10);
    System.out.println(dog.getAge());
    dog.printlnfo();
    dog.eat();
    Cat2 cat = new Cat2();
    cat.setName("猫");
    System.out.println(cat.getName());
    cat.setColor("白");
    System.out.println(cat.getColor());
    cat.setAge(5);
    System.out.println(cat.getAge());
    cat.printlnfo();
    cat.eat();
}

}
/* hao
* 2018-2-15 10:47:15
* 人类
*/
public class Person {
String name;
int code;
String type;

Person(){

}
Person(String name,int code,String type){
    this.name = name;
    this.code = code;
    this.type = type;
}
void speak(){
    System.out.println("爱说");
}
void eat(){
    System.out.println("爱吃");
}

}

public class Worker2 extends Person {
Worker2(String name,int code,String type){
this.name = name;
this.code = code;
this.type = type;
}
void print(){
System.out.println(name);
System.out.println(code);
System.out.println(type);
}
void speak(){
System.out.println(“好说”);
}
void eat(){
System.out.println(“好吃”);
}
}

public class TestPerson {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Worker2 wo = new Worker2("吴老二",111,"好人");
    wo.speak();
    wo.eat();
    wo.print();
    Worker2 rk = new Worker2("吴老三",222,"大人");
    rk.speak();
    rk.eat();
    rk.print();
}

}
/* hao
* 2018-2-15 11:43:54
* calc
*/
public class Calc {
String color;
int price;

public String getColor() {
    return color;
}


public void setColor(String color) {
    this.color = color;
}


public int getPrice() {
    return price;
}


public void setPrice(int price) {
    this.price = price;
}


void show(){
    System.out.println("计算展示");
}

}

public class Phone extends Calc {
void call(){
System.out.println(“打电话”);
}
void callto(){
System.out.println(“接电话”);
}
}

public class TestCalc {

public static void main(String[] args) {
    Phone ph = new Phone();
    ph.setColor("磨砂黑");
    System.out.println(ph.getColor());
    ph.setPrice(8888);
    System.out.println(ph.getPrice());

    Phone on = new Phone();
    on.setColor("中国红");
    System.out.println(on.getColor());
    on.setPrice(6666);
    System.out.println(on.getPrice());
}

}

当你最认为困难的时候,其实就是你最接近成功的时候。
《当幸福来敲门》

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值