java基础语法-day16-super关键字

本文介绍了Java中的super关键字在继承关系中的使用,包括构造方法的调用、成员变量和方法的访问。通过Account和CreditAccount类的示例展示了super的运用,以及在子类构造方法中调用父类构造方法的执行顺序。此外,还探讨了super与this的区别,强调super不能在静态方法中使用,并给出了一个关于super在方法重写中的实例。文章最后提供了一个猜数字游戏的代码,进一步说明了super的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

p465 super关键字

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

p466 super关键字练习

在这里插入图片描述
在这里插入图片描述
执行顺序13654

p467 回顾上午内容

p468 super实参的用法

  • Account
package com.bjpowernode.javase.test001;

public class Account {
	
	private String actno;
	private double balance;
	//构造方法
	public Account() {
		super();
	}
	public Account(String actno, double balance) {
		super();
		this.actno = actno;
		this.balance = balance;
	}
	//setter和getter
	public String getActno() {
		return actno;
	}
	public void setActno(String actno) {
		this.actno = actno;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	
}

  • CreditAccount
package com.bjpowernode.javase.test001;

public class CreditAccount extends Account{
	private double credit;

	//构造方法
	public CreditAccount(double credit) {
//		super("ZhonejieMa",31.0);
		this.credit = credit;
	}
	
	public CreditAccount() {

	}

	public CreditAccount(String actno,double balance,double credit) {
		super(actno,balance);
		//私有变量子类不能访问
//		this.actno = actno;
//		this.balance = balance;
		this.credit = credit;
	}

	
	
	//setter和getter
	public double getCredit() {
		return credit;
	}

	public void setCredit(double credit) {
		this.credit = credit;
	}
}

  • test
package com.bjpowernode.javase.test001;

public class ExtendsTest {
	
	public static void main(String[] args) {
		
		
		CreditAccount c1 = new CreditAccount();
		CreditAccount c2 = new CreditAccount("ZhonejieMa",31.0,3.0);
		
		System.out.println(c1.getActno());
		System.out.println(c1.getBalance());
		System.out.println(c1.getCredit());
		                    
		System.out.println(c2.getActno());
		System.out.println(c2.getBalance());
		System.out.println(c2.getCredit());
		
	}
}

在这里插入图片描述

p469 上一篇的内存图

在这里插入图片描述

p470 内存图描述super

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

p471 VIP购物说明super

先思考:
在这里插入图片描述

在这里插入图片描述

p472 VIP2购物说明super .什么时候不能省略

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

p473 super不能单独使用,必须是super.xxx

在这里插入图片描述

super和this 不能用在static方法中

在这里插入图片描述

p474 super和this方法重写

在这里插入图片描述

在这里插入图片描述

package com.bjpowernode.javase.test001;

public class SuperTest07 {

	public static void main(String[] args) {
		Cat c = new Cat();
		c.yiDong();
	}
}
class Animal{
	public void move() {
		System.out.println("Animal move");
	}
}


class Cat extends Animal{
	public void move() {
		System.out.println("Cat move");
	}
	public void yiDong() {
		this.move();
		move();
		super.move();
	}
}

p475 super总结

在这里插入图片描述

p476 猜数字游戏

package com.bjpowernode.javase.test002;

public class GuessNumber {

	public static void main(String[] args) {
		
		//创建A对象
		A a = new A(100);
		
		//创建B对象
		B b = new B(a);
		
		//开始猜测
		
		java.util.Scanner s = new java.util.Scanner(System.in);
		while (true) {
			System.out.println("请输入猜测值:");
			int guessNum = s.nextInt();
			b.guess(guessNum);
		}
		
	}
}

class A{
	private int v;

	public A() {
		super();
	}
	
	public A(int v) {
		super();
		this.v = v;
	}

	public int getV() {
		return v;
	}

	public void setV(int v) {
		this.v = v;
	}
	
	
}

class B{
	//把A作为B的实例变量
	//通过B猜测A,则B中要有A的对象
	private A a;

	public B(A a) {
		super();
		this.a = a;
	}

	public B() {
		super();
	}

	public A getA() {
		return a;
	}

	public void setA(A a) {
		this.a = a;
	}
	
	//猜测的方法
	public void guess(int guessNum){
		//实际的数字
		int trueNum = this.getA().getV();
		//int trueNum = a.getV();
		if (guessNum == trueNum) {
			System.out.println("猜对了");
			System.exit(0);
		}else if (guessNum > trueNum) {
			System.out.println("猜大了");
		}else {
			System.out.println("猜小了");
		}
	}
	
}

p477-480 idea的使用

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

p481 交通工具类例子day16homework2

在这里插入图片描述

public class HomeWork2 {
    public static void main(String[] args) {
//        通过有参数构造方法创建对象
        Vehicle v1 = new Vehicle(122,4);
//        通过无参数构造方法创建对象,通过setter和getter赋值
        Vehicle v2 = new Vehicle();
        v2.setSize(5);
        v2.setSpeed(0);
        System.out.println("size:"+v2.getSize());
        System.out.println("speed:"+v2.getSpeed());

        v2.move();
        //调用加速方法
        v2.speedUp(80);
        System.out.println("speed:"+v2.getSpeed());

        //调用减速方法
        v2.speedDown(25);
        System.out.println("speed:"+v2.getSpeed());
    }
}
//交通工具
class Vehicle{
    //速度
    private int speed;
    //体积
    private int size;

    public int getSpeed() {
        return speed;
    }

    public int getSize() {
        return size;
    }

    //设置速度的方法
    public void setSpeed(int speed) {
        this.speed = speed;
    }

    //设置体积的方法
    public void setSize(int size) {
        this.size = size;
    }
//    有参数构造方法
    public Vehicle(int speed, int size) {
        this.speed = speed;
        this.size = size;
    }

    public Vehicle() {
    }

    //    移动方法
    public void move(){
        System.out.println("公共汽车,起步行驶");
    }
//    加速方法
    public void speedUp(int addspeed){
        //在原来速度的基础上加
        this.setSpeed(this.getSpeed() + addspeed);
    }
//    减速方法
    public void speedDown(int subspeed){
        //在原来速度的基础上减
        //最好有判断,小于0不能再减
        this.setSpeed(this.getSpeed() - subspeed);
    }
}

p482计算器例子day16homework4

在这里插入图片描述

public class HomeWork4 {
    public static void main(String[] args) {
        Number number = new Number(10,20);

        //计算
        number.addition();
        number.subtration();
        number.multipication();
        number.division();
    }
}
class Number{
    private int n1;
    private int n2;

    public Number() {
    }

    public Number(int n1, int n2) {
        this.n1 = n1;
        this.n2 = n2;
    }

    public void setN1(int n1) {
        this.n1 = n1;
    }

    public void setN2(int n2) {
        this.n2 = n2;
    }

    public int getN1() {
        return n1;
    }

    public int getN2() {
        return n2;
    }

    //返回值可以是void,直接输出
    //加法
    public void addition(){
        System.out.println(this.getN1()+"+"+this.getN2()+"="+(this.getN1()+this.getN2()));
    }
    //减法
    public void subtration(){
        int result = (this.getN1() - this.getN2());
        System.out.println(n1+"-"+n2+"="+result);

    }
    //乘法
    public void multipication(){
        int result = n1 * n2;
        System.out.println(n1+"*"+n2+"="+result);
    }
    //除法
    public void division(){
        if (n2 == 0){
            System.out.println("除数不能为0");
            return ;
        }
        int result = n1/n2;
        System.out.println(n1+"/"+n2+"="+result);
    }
}

p483 人类例子day16homework5

在这里插入图片描述

public class HomeWork5 {
    public static void main(String[] args) {
        Person p1 = new Person("ZhonejieMa",24);
        p1.display();

        Person p2 = new Person();
        p2.setName("JinweiHa");
        p2.setAge(23);
        p2.display();

        Person p3 = new Person();
        p3.display();


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

    public Person() {
    }

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

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

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void display(){
//        System.out.println("姓名:"+this.getName());
        //因为name和age虽然在本类中是私有的,但是可以直接访问
        System.out.println("姓名:" + name+","+"年龄:"+age);


    }

}

p484 时间例子day16homework3在这里插入图片描述

public class HomeWork3 {
    public static void main(String[] args) {
        MyTime time1 = new MyTime();
        time1.display();

        MyTime time2 = new MyTime(17,06,50);
        time2.display();
        time2.addSecond(600);
        time2.display();
/*
        time2.addMinute(50);
        time2.display();*/

    }
}
class MyTime{
    private int hour;
    private int minute;
    private int second;

    public MyTime() {
    }

    public MyTime(int hour, int minute, int second) {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public void setMinute(int minute) {
        this.minute = minute;
//        System.out.println(minute);
    }

    public void setSecond(int second) {
        this.second = second;
    }

    public int getHour() {
        return hour;
    }

    public int getMinute() {
        return minute;
    }

    public int getSecond() {
        return second;
    }

    //这个题目主要就是锻炼set和get方法
    //要知道set方法就是赋值,get方法就是读取。
    public void addSecond(int sec){
        int oldsec = this.getSecond();//50
        int newsec = oldsec + sec;//50+10=60
//        System.out.println(newsec);
        if (newsec < 60){
            this.setSecond(this.getSecond() + sec);
        }else if(newsec == 60){
            //分钟加1
            this.addMinute(1);
            this.setSecond(0);
        }else{
            //总秒数大于60
            int newMinute = newsec/60;
            this.addMinute(newMinute);
            this.setSecond(newsec%60);
        }

    }
    public void addMinute(int min){
        int oldMin = this.getMinute();//6
        int newMin = oldMin + min;//6+1=7
//        System.out.println(newMin);
        if (newMin < 60){
            this.setMinute(newMin);
//            System.out.println("min:"+newMin);

        }else if(newMin == 60){
            this.addHour(1);
            this.setMinute(0);
        }else{
            int newHour = newMin / 60;
            this.addHour(newHour);
            this.setMinute(newMin % 60);
        }
    }
    public void addHour(int hou){
        this.setHour(this.getHour() + hou);
    }

    public void subSecond(int sec){
        this.setSecond(this.getSecond()- sec);
    }
    public void subMinute(int min){
        this.setMinute(this.getMinute()- min);
    }
    public void subHour(int hou){
        this.setHour(this.getHour() - hou);
    }

    public void display(){
        System.out.println(this.getHour()+":"+this.getMinute()+":"+this.getSecond());
    }
}

进阶

在这里插入图片描述

day20

p485-p492 final关键字前面学过了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值