super关键字
super能出现在实例方法和构造方法中。
super的语法是:“super.”、“super()”
super不能使用在静态方法中。
super. 大部分情况下是可以省略的。
super.什么时候不能省略呢?
父类和子类中有同名属性,或者说有同样的方法,想在子类中访问父类的,super. 不能省略。
super() 只能出现在构造方法第一行,通过当前的构造方法去调用“父类”中
的构造方法,目的是:创建子类对象的时候,先初始化父类型特征。
super的使用:
super.属性名 【访问父类的属性】
super.方法名(实参) 【访问父类的方法】
super(实参) 【调用父类的构造方法】
代码示例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
public class SuperTest01{ public static void main(String[] args){
new B(); } }
class A extends Object{
public A(){ System.out.println("A类的无参数构造方法!"); }
public A(int i){ System.out.println("A类的有参数构造方法(int)"); } }
class B extends A{
public B(){ this("zhangsan"); System.out.println("B类的无参数构造方法!"); }
public B(String name){ super(); System.out.println("B类的有参数构造方法(String)"); } }
|
代码示例2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
public class SuperTest03{ public static void main(String[] args){
CreditAccount ca1 = new CreditAccount(); System.out.println(ca1.getActno() + "," + ca1.getBalance() + "," + ca1.getCredit());
CreditAccount ca2 = new CreditAccount("1111", 10000.0, 0.999); System.out.println(ca2.getActno() + "," + ca2.getBalance() + "," + ca2.getCredit());
} }
class Account extends Object{ private String actno; private double balance;
public Account(){ } public Account(String actno, double balance){ this.actno = actno; this.balance = balance; }
public void setActno(String actno){ this.actno = actno; } public String getActno(){ return actno; } public void setBalance(double balance){ this.balance = balance; } public double getBalance(){ return balance; } }
class CreditAccount extends Account{
private double credit;
public CreditAccount(String actno, double balance, double credit){
super(actno, balance); this.credit = credit; }
public CreditAccount(){ }
public void setCredit(double credit){ this.credit = credit; } public double getCredit(){ return credit; } }
|
代码示例2的JVM体现:
super不能省略时的JVM体现:
分享几道作业题
猜数字:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
import java.util.Scanner; public class HomeWork4GuessNumber { public static void main(String[] args) { A a = new A(100); B b = new B(a); Scanner s = new Scanner(System.in); while(true) { O.p("请输入要猜测的数字:"); int num = s.nextInt(); b.guess(num); } } }
class A { public int getV() { return v; }
private int v; public A() {}
public A(int v) { this.v = v; } }
class B { private A a; public B() {} public B(A a) { this.a = a; }
public void guess(int num) { int realNum = a.getV(); if(realNum == num) { O.p("猜对啦!"); System.exit(0); } else if(realNum > num) { O.p("猜小了"); } else{ O.p("猜大了"); } }
}
|
加减速:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
import java.util.Scanner; public class HomeWork5Transport { public static void main(String[] args) { Vehicle v = new Vehicle(); Scanner s = new Scanner(System.in); O.p("请输入初始速度"); v.setSpeed(s.nextInt()); O.p("请输入初始体积"); v.setSize(s.nextInt()); O.p("初始速度为" + v.getSpeed()); O.p("初始体积为" + v.getSize()); int i,u=1; while(u == 1) { O.p("输入1进行加速,输入2进行减速"); i = s.nextInt(); if (i == 1) { v.setSpeed(v.speedUp(v.getSpeed())); } else { v.setSpeed(v.speedDown(v.getSpeed())); } O.p("此时的速度为" + v.getSpeed()); O.p("输入1将继续加/减速,输入2将自动结束程序"); u = s.nextInt(); } } }
class Vehicle { public int getSpeed() { return speed; }
public void setSpeed(int speed) { this.speed = speed; }
private int speed; public int getSize() { return size; } public void setSize(int size) { this.size = size; } private int size ;
public int speedUp(int v) { v++; return v; }
public int speedDown(int v) { v--; return v; } }
|
增减时间:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
import java.util.Scanner; public class HomeWork6Time { public static void main(String[] args){ MyTime t = new MyTime(); Scanner s = new Scanner(System.in); O.p("请输入初始小时"); t.setHour(s.nextInt()); O.p("请输入初始分钟"); t.setMinute(s.nextInt()); O.p("请输入初始秒数"); t.setSecond(s.nextInt()); O.p("您输入为" + t.getHour() + "小时" + t.getMinute() + "分钟" + t.getSecond() + "秒数"); int i=1; while(i > 0 && i <7) { O.p("增加1s请输入1,增加1min请输入2,增加1h请输入3"); O.p("减少1s请输入4,减少1min请输入5,减少1h请输入6,输入其他数字退出程序"); i = s.nextInt(); if (i == 1) t.setSecond(t.addSecond(t.getSecond(),t.getMinute(),t.getHour())); else if (i == 2) t.setMinute(t.addMinute(t.getMinute(),t.getHour())); else if (i == 3) t.setHour(t.addHour(t.getHour())); else if (i == 4) t.setSecond(t.subSecond(t.getSecond(),t.getMinute(),t.getHour())); else if (i == 5) t.setMinute(t.subMinute(t.getMinute(),t.getHour())); else if (i == 6) t.setHour(t.subHour(t.getHour()));
O.p("时间为" + t.getHour() + "小时" + t.getMinute() + "分钟" + t.getSecond() + "秒数"); } } }
class MyTime { public int getHour() { return hour; }
public void setHour(int hour) { this.hour = hour; }
private int hour;
public int getMinute() { return minute; }
public void setMinute(int minute) { this.minute = minute; }
private int minute;
public int getSecond() { return second; }
public void setSecond(int second) { this.second = second; }
private int second;
public int addSecond(int s,int m,int h) { s++; if (s == 60) { s = 0; this.setMinute(addMinute(m,h)); } return s;
}
public int addMinute(int m,int h) { m++; if (m == 60) { m = 0; this.setHour(addHour(h));
} return m; }
public int addHour(int h) { h++; return h; }
public int subHour(int h) { h--; return h; }
public int subMinute(int m,int h) { m--; if(m<0) { m=59; this.setHour(subHour(h)); } return m; }
public int subSecond(int s,int m,int h) { s--; if(s<0) { s=59; this.setMinute(subMinute(m,h)); } return s; } }
|