JAVA第四天实训纪录

一.继承
发生在父子类之间

在Java语言中使用extends(扩展)关键字来表示继承关系。

(1)子类不能继承父类的构造方法和私有方法,但私有成员变量可以被继承只是不能直接访问。

(2)无论使用何种方式构造子类的对象时都会自动调用父类的无参构造方法,来初始化从父类

   中继承的成员变量,相当于在构造方法的第一行增加代码:super()的效果。

(3)使用继承必须满足逻辑关系:子类 is a 父类,也就是不能滥用继承。

(4)Java语言中只支持单继承不支持多继承,也就是说一个子类只能有一个父类,但一个父类

   可以有多个子类。

public class Person {
    private String name;
 
 
    private int age;
 
    public Person() {
        System.out.println("父类无参构造方法!");
    }
 
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public void eat(){
 
  }
  public void play(){
 
  }
 
}

public class Student extends Person {
 
    private int studentId;
 
    public Student() {
        super();
 
    }
 
    public Student(int studentId) {
        this.studentId = studentId;
    }
 
    public Student(String name, int age, int studentId) {
        super(name, age);
        this.studentId = studentId;
    }
 
    public void study() {
 
    }
 
}

public class Teacher extends Person {
    private int teacherId;
    public void teaching(){
 
    }
}
public class Worker extends Person {
    private double salary;
 
    public void working() {
 
    }
}
public class PersonTest {
    public static void main(String[] args) {
        Student student = new Student();
    }
}
 

二.方法的重写
1.方法的重载(方法名相同,参数类型,个数,顺序不同)
2.重写的原则
   a.要求方法名相同、参数列表相同以及返回值类型相同,从jdk1.5开始允许返回子类类型。

   b.要求方法的访问权限不能变小,可以相同或者变大。

   c.要求方法不能抛出更大的异常(异常机制)。

   d.声明为static和private的方法不能被重写

3.重写与重载的区别


4.常用的访问权限
 访问控制符    访问权限     本类内部     本包中的类     子类内部    其它包中的类

    public          公有的            ok                ok                     ok                  ok

  protected      保护的            ok               ok                      ok                  no

   啥也不写       默认的           ok               ok                      no                 no

    private         私有的            ok               no                      no                 no

练习:
(1)

package Animal;
 
public class Animal {
    private String name;
    private String color;
 
    public Animal() {
    }
 
    public Animal(String name, String color) {
        this.name = name;
        this.color = color;
    }
 
    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 void show() {
        System.out.println("我的名字叫:" + getName() + "我的颜色是:" + getColor() + "色");
    }
 
}

package Animal;
 
public class Dog extends Animal {
    private int teethNumber;
 
    public Dog() {
    }
 
    public Dog(String name, String color, int teethNumber) {
        super(name, color);
        this.teethNumber = teethNumber;
    }
 
    public int getTeethNumber() {
        return teethNumber;
    }
 
    public void setTeethNumber(int teethNumber) {
        this.teethNumber = teethNumber;
    }
 
    @Override
    public void show() {
        System.out.println("我叫:" + getName() + ",我的颜色是:" + getColor() + ",我的牙齿数量是:" + getTeethNumber() + "颗");
    }
 
}

package Animal;
 
public class DogTest {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.show();
        Dog dog1 = new Dog("亚利", "粉色", 1);
        dog1.show();
    }
}


 (2)

package shape;
 
public class Shape {
    private double x;
    private double y;
 
    public Shape() {
    }
 
    public Shape(double x, double y) {
        this.x = x;
        this.y = y;
    }
 
    public double getX() {
        return x;
    }
 
    public void setX(double x) {
        this.x = x;
    }
 
    public double getY() {
        return y;
    }
 
    public void setY(double y) {
        this.y = y;
    }
 
 
}

package shape;
 
public class Rect extends Shape {
 
    private double length;
    private double width;
 
    public Rect() {
    }
 
    public Rect(double x, double y, double length, double width) {
        super(x, y);
        this.length = length;
        this.width = width;
    }
 
    public double getLength() {
        return length;
    }
 
    public void setLength(double length) {
        this.length = length;
    }
 
    public double getWidth() {
        return width;
    }
 
    public void setWidth(double width) {
        this.width = width;
    }
    public void show() {
        System.out.println("矩形的横纵坐标:("+super.getX()+","+super.getY()+")"+" 矩形的长:"+getLength()+" 矩形的宽:"+getWidth());
    }
}

package shape;
 
public class Circle extends Shape {
    private double r;
 
    public Circle() {
    }
 
    public Circle(double x, double y, double r) {
        super(x, y);
        this.r = r;
    }
 
    public double getR() {
        return r;
    }
 
    public void setR(double r) {
        this.r = r;
    }
    public void show() {
        System.out.println("圆的横纵坐标:("+super.getX()+","+super.getY()+")"+" 圆的半径:"+getR());
    }
}

package shape;
 
public class ShapTest {
    public static void main(String[] args) {
        Rect rect = new Rect();
        rect.show();
        Circle circle = new Circle();
        circle.show();
        Rect rect1 = new Rect(3, 4, 5, 6);
        rect1.show();
        Circle circle1 = new Circle(1, 2, 3);
        circle1.show();
 
 
    }
}

 三.Final


 四.多态
(1)

package person03;
 
public class Person {
    private String name;
    private int age;
 
    public Person() {
    }
 
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public void show(){
        System.out.println("姓名:"+getName()+" 年龄:"+getAge());
    }
}

package person03;
 
public class Worker extends Person{
    private double sal;
 
    public Worker() {
    }
 
    public Worker(String name, int age, double sal) {
        super(name, age);
        this.sal = sal;
    }
 
    public double getSal() {
        return sal;
    }
 
    public void setSal(double sal) {
        this.sal = sal;
    }
    @Override
    public void show(){
        System.out.println("姓名:"+super.getName()+" 年龄:"+super.getAge()+" 薪水为:"+getSal());
    }
 
}

package person03;
 
public class PersonWorkerTest {
    public static void main(String[] args) {
        Person person = new Person("gao",18);
        Worker worker = new Worker("gao",18,6000);
        person.show();
        worker.show();
        Person p = new Worker("gao",18,6000);
        p.show();
    }
}
 

 (2)

package shape;
 
public class Shape {
    private double x;
    private double y;
 
    public Shape() {
    }
 
    public Shape(double x, double y) {
        this.x = x;
        this.y = y;
    }
 
    public double getX() {
        return x;
    }
 
    public void setX(double x) {
        this.x = x;
    }
 
    public double getY() {
        return y;
    }
 
    public void setY(double y) {
        this.y = y;
    }
 
    public void show() {
        System.out.println("圆的横纵坐标:(" + getX() + "," + getY() + ")");
    }
 
}

package shape;
 
public class Rect extends Shape {
 
    private double length;
    private double width;
 
    public Rect() {
    }
 
    public Rect(double x, double y, double length, double width) {
        super(x, y);
        this.length = length;
        this.width = width;
    }
 
    public double getLength() {
        return length;
    }
 
    public void setLength(double length) {
        this.length = length;
    }
 
    public double getWidth() {
        return width;
    }
 
    public void setWidth(double width) {
        this.width = width;
    }
 
    @Override
    public void show() {
        System.out.println("矩形的横纵坐标:(" + super.getX() + "," + super.getY() + ")" + " 矩形的长:" + getLength() + " 矩形的宽:" + getWidth());
    }
}

package shape;
 
public class Circle extends Shape {
    private double r;
 
    public Circle() {
    }
 
    public Circle(double x, double y, double r) {
        super(x, y);
        this.r = r;
    }
 
    public double getR() {
        return r;
    }
 
    public void setR(double r) {
        this.r = r;
    }
 
    @Override
    public void show() {
        System.out.println("圆的横纵坐标:(" + super.getX() + "," + super.getY() + ")" + " 圆的半径:" + getR());
    }
}

​​
package shape;
 
 
public class ShapTest {
    public static void printRect(Rect rect) {
        rect.show();
    }
 
    public static void printCircle(Circle circle) {
        circle.show();
    }
 
    //既能打印矩形又能打印圆形
    public static void printShape(Shape shape) {
        shape.show();
 
    }
 
    public static void main(String[] args) {
        Rect rect = new Rect();
        rect.show();
        Circle circle = new Circle();
        circle.show();
        Rect rect1 = new Rect(3, 4, 5, 6);
        rect1.show();
        Circle circle1 = new Circle(1, 2, 3);
        circle1.show();
        Shape p = new Rect(3, 4, 5, 6);
        Shape s = new Circle(1, 2, 3);
        p.show();
        s.show();
        printRect(new Rect(3, 4, 5, 6));
        printCircle(new Circle(1, 2, 3));
        printShape(new Rect(3, 4, 5, 6));
        printShape(new Circle(1, 2, 3));
    }
}

五.抽象类抽象方法
(1)

package person02;
 
public abstract class Person {
  private String name;
  private int age;
  private double height;
 
    public Person() {
        System.out.println("Person构造方法被调用");
    }
    public abstract void say();
 
}
package person02;
 
public class Nurse extends Person {
    public Nurse() {
        System.out.println("Nurse构造方法被调用");
    }
 
    @Override
    public void say() {
 
    }
}
package person02;
 
public class PersonTest {
    public static void main(String[] args) {
        Nurse nurse = new Nurse();
 
    }
}


 (2)

package account;
 
public abstract class Account {
    private double balance;
 
    //计算利息的抽象方法
    public abstract double getInterest(double index, double time);
 
    public Account() {
    }
 
    public Account(double balance) {
        this.balance = balance;
    }
 
    public double getBalance() {
        return balance;
    }
 
    public void setBalance(double balance) {
        this.balance = balance;
    }
}

package account;
 
public class FixesAccount extends Account {
 
    public FixesAccount() {
    }
 
    public FixesAccount(double balance) {
        super(balance);
    }
 
    @Override
    public double getInterest(double index, double time) {
 
        return getBalance() * index * time;
    }
}

package account;
 
public class AccountTest {
    public static void main(String[] args) {
        Account account = new FixesAccount(1000);
        System.out.println("本金:" + account.getBalance() + ",利息为:" + account.getInterest(0.003, 1));
    }
}


 六.接口
//接口本身都是有全局常量和抽象方法组成
//public static final     public abstract  可省略


package runner;
 
public interface Runner {
    void running();
 
}
package runner;
 
public interface Hunter extends Runner {
    void hunt();
 
}
package runner;
 
public class Chinese implements Hunter {
 
    @Override
    public void hunt() {
        System.out.println("抓到一只小白兔");
    }
 
    @Override
    public void running() {
        System.out.println("正在全力奔跑");
 
    }
 
}

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

————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/2201_75588545/article/details/135835419

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值