Java实训第四天

本文通过实例介绍了Java中的封装、继承、多态以及抽象类、接口等面向对象编程的核心概念,展示了如何创建类、继承、多态地使用以及抽象方法的运用。
摘要由CSDN通过智能技术生成

封装、继承、多态:

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
    }

    public void eat() {
        System.out.println("我叫"+name+" 刚满"+age+" 并且我会吃饭");
    }
}
public class Student extends Person{
    private long studentId;

    public Student(String name, int age) {
        super(name, age);
    }

    public Student(String name, int age, long studentId) {
        super(name, age);
        this.studentId = studentId;
    }

    public void study(){
        System.out.println("我爱学习");
    }
}
package com.java.animal;

public abstract class Animal {

    private String name;
    private String color;

    public Animal() {
    }

    public Animal(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public String name() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String color() {
        return color;
    }

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

    public void show(){
        System.out.println("我的名字叫" + name() + "。我的颜色是" + color());
    }


}
package com.java.animal;

public class Dog extends Animal{
    private int teethNumber;

    public Dog() {
    }

    public Dog(int teethNumber) {
        this.teethNumber = teethNumber;
    }

    public Dog(String name, String color) {
        super(name, color);
    }

    public Dog(String name, String color, int teethNumber) {
        super(name, color);
        this.teethNumber = teethNumber;
    }

    public int teethNumber() {
        return teethNumber;
    }

    public void setTeethNumber(int teethNumber) {
        this.teethNumber = teethNumber;
    }

    @Override
    public void show(){
        System.out.println("我的名字是" + name() + ",我的颜色是" + color() + ",我的牙齿有" + teethNumber() +"颗");
    }
}
package com.java.animal;

public class Dogtest extends Dog {
    public static void main(String[] args) {
        Dog d = new Dog("lxp","黑" ,0);
        d.show();
    }
}

public class Rect extends Shape{
    private double length,width;

    public Rect() {
    }

    public Rect(double x, double length, double width) {
        super(x);
        this.length = length;
        this.width = width;
    }

    public double length() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double width() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    @Override
    public void show(){
        System.out.println();
    }

}
public class Circle extends Shape{
    private double r;

    public Circle() {
    }

    public Circle(double x, double r) {
        super(x);
        this.r = r;
    }

    public double r() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }

    @Override
    public void show(){
        System.out.println();
    }
}
public class Shape {
    private double x;

    public Shape() {
    }

    public Shape(double x) {
        this.x = x;
    }

    public double x() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void show(){
        System.out.println();
    }
}
package com.java.animal;

public class ShapeTest {

    public static void printCircle(Circle circle){
        circle.show();
    }
    public static void printRect(Rect rect){
        rect.show();
    }
    //即打印圆形也打印矩形
    public static void printShape(Shape shape){
        shape.show();

    }

    public static void main(String[] args) {
        Shape s = new Shape();
        s.show();

        Circle c = new Circle();
        c.show();

        Rect r = new Rect();
        r.show();

        printCircle(new Circle());
        printRect(new Rect());
        printShape(new Shape());

        //父类型 引用变量名 = new 子类类型();
        Shape shape = new Rect();
        shape.show();
        Shape shape1 = new Circle();
        shape1.show();


        printShape(new Rect());
        /*
        相当于
        Shape shape = new Rect();
        printShape(new Rect());
        * */
        printShape(new Circle());
    }
}
package com.java.abstracttest;

public abstract class Person {//static修饰的方法只能调用静态的方法
    private String name;
    private int age;
    private double height;

    public Person() {
    }

    public void  say() {
    }
}
package com.java.abstracttest;

public class Account {
    private double balance;

    public Account() {
    }

    public Account(double balance) {
        this.balance = balance;
    }

    public double balance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }


    public void money(){
        System.out.println(balance());
    }
}
package com.java.abstracttest;

public class FixedAccount extends Account{

    public FixedAccount() {
    }

    public FixedAccount(double balance) {
        super(balance);
    }

    @Override
    public void money(){
        System.out.println(balance() * 0.003 * 1);
    }

}
package com.java.abstracttest;

public class FixedAccountTest {


    public static void main(String[] args) {
        Account a = new FixedAccount(1000);
        a.money();


        FixedAccount f = new FixedAccount(1000);
        System.out.println(f.balance());

    }

}
package com.java.finaltest.FinalDemo01;
//final修饰 属性(成员变量)【常量】局部变量可以付一次值
// 方法【不可被重写】
// 类【不可被继承】
public  class FinalDemo {

    //final int b;Variable 'b' might not have been initialized
    final int b = 1;

    public static void main(String[] args) {
        final int a;
        a = 1;
        //a = 2;//Variable 'a' might already have been assigned to
    }

}
package com.java.interfacetest;

public class Chinese implements Hunter{
    @Override
    public void run() {
        System.out.println("正在奔跑");
    }

    @Override
    public void hunting() {
        System.out.println("正在捕猎");
    }
}
package com.java.interfacetest;

public class ChineseTest {
    public static void main(String[] args) {
        //接口类型的引用指向实现类的对象,形成多态
        Runner runner = new Chinese();
        runner.run();
        Hunter hunter = new Chinese();
        hunter.hunting();
    }
}
package com.java.interfacetest;

public interface Hunter extends Runner {
    @Override
    void run();

    void hunting();
}
package com.java.interfacetest;

public interface Runner {
    void run();
}
package com.java.polymorphism;

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }

    public String name() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int age() {
        return age;
    }

    public void setAge(int aeg) {
        this.age = aeg;
    }

    public void show(){
        System.out.println("我的名字是" + name() + ",我的年龄是" + age());
    }
}
package com.java.polymorphism;

public class PersonWorkerTest {


    public static void main(String[] args) {

        Person p = new Person("jxr",20);
        Worker w = new Worker("jxr",20,1500.0);
        p.show();
        w.show();

        Person pw = new Worker("lxp",20,100.0
        );

        pw.show();//编译阶段调用person的show方法,运行阶段调用worker的show方法
    }

}
package com.java.polymorphism;

public class Worker extends Person{
    private double pay;

    public Worker(){
    }
    public Worker(String name, int age) {
        super(name, age);
    }

    public Worker(String name, int age, double pay) {
        super(name, age);
        this.pay = pay;
    }

    public double pay() {//普通成员方法属于对象层级,静态成员方法属于类层级
        return pay;
    }

    public void setPay(double pay) {
        this.pay = pay;
    }

    @Override
   public void show(){
        System.out.println("我的名字是" + name() + ",我的年龄是" + age() + ",我薪水是" + pay());
    }
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值