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());
}
}