java基础回顾
//通过super调用父类构造方法和隐藏方法
package mypack; class Father { public Father() { // TODO Auto-generated constructor stub System.out.println("null father"); } public Father(String name) { System.out.println("name = " + name); } public String toString() { // TODO Auto-generated method stub return "我是father"; } } class Sun extends Father { int age = 20; public Sun() { // TODO Auto-generated constructor stub super("铁鹏"); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { // TODO Auto-generated method stub return super.toString(); } } public class DemoFive { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Sun sun = new Sun(); System.out.println(sun.toString()); ; } }
枚举回顾
package reviewDemo; //枚举 enum Color{ Green,Blue,Yellow; @Override public String toString() { String ret = super.toString(); switch (this) { case Green: ret = "绿色"; break; case Blue: ret = "蓝色"; break; case Yellow: ret = "黄色"; break; default: break; } return ret; } } class Personp{ Color c = Color.Blue; void show(){ System.out.println(c); } } public class Demo18 { public static void main(String[] args) { Color []color = Color.values(); for (Color c : color) { System.out.println(c); } new Personp().show(); } } 输出: 绿色 蓝色 黄色 蓝色
枚举继承接口
interface I { void show(); } enum Color implements I { RED() { public void show() { } }, GREEN { public void show() { } }, BLUE { public void show() { } }; } enum Color0 implements I { RED(), GREEN, BLUE; public void show() { } }
this语句的用法
package pack; //this语句用于构造函数之间相互调用,代表某个对象 //this只能放在构造函数的第一行 class Person{ private String name; private int age; Person(){ } Person(String name){ this(); this.name = name; } Person(String name,int age){ this(name); //this语句只用于调用其他构造方法,这个是调用Person(name), // Person(name); //这条语句是非法的 this.age = age; } } public class Demo{ public static void main(String[] args) { StringBuffer sb = new StringBuffer(); } }