this修饰方法
this.
可以省略不写public class Person {//属性int age;String name;double height;//空构造器public Person() {}//有参数构造器public Person(int age, String name, double height) {this.age = age;this.name = name;this.height = height;}public void gem(){pla();this.pla();//System.out.println("上网");System.out.println("洗剪吹");}public void pla(){System.out.println("上网");System.out.println(age);System.out.println(this.age);}
this修饰属性
this.
修饰,如果访问属性不发生重名问题this.
也可以省略不写public class Person {//属性int age;String name;double height;//空构造器public Person() {}//有参数构造器public Person(int age, String name, double height) {this.age = age;this.name = name;this.height = height;}public void eat(){int age = 17;System.out.println(age);//就近原则,局部变量的ageSystem.out.println(this.age);//属性赋值的ageSystem.out.println("蹭饭");}
}
this修饰构造器
public class Person {//属性int age;String name;double height;//空构造器public Person() {}//有参数构造器public Person(int age, String name, double height) {this(age, name);//此处调用下面有两个参数的构造器,此代码必须写在第一行this.age = age;this.name = name;this.height = height;}public Person(int age, String name) {this(age);//此处调用下面的只要一个参数的构造器里面的age,此代码必须写在第一行this.age = age;this.name = name;}public Person(int age) {this.age = age;}
static修饰属性
public class Test {//属性int id;static int sid;public static void main(String[] args) {Test.sid = 113;System.out.println(Test.sid);//创建具体对象Test test1 = new Test();test1.id = 11;test1.sid = 11;Test test2 = new Test();test2.id = 21;test2.sid = 21;Test test3 = new Test();test3.id = 31;test3.sid = 31;//读取属性值System.out.println(test1.id);System.out.println(test2.id);System.out.println(test3.id);System.out.println(test1.sid);System.out.println(test2.sid);System.out.println(test3.sid);}
}
static修饰方法
public class Demo {int id;static int sid;public void de(){System.out.println(id);System.out.println(sid);System.out.println("--de---");}//static和public都是修饰符,并列时没有先后顺序public static void ae(){//System.out.println(id); 在静态方法中不能访问非静态属性//在静态方法中不能访问非静态方法/*this.de();de();*///System.out.println(this.id); 在静态方法中不能使用this关键字System.out.println(sid);System.out.println("--ae---");}public static void main(String[] args) {//非静态方法可以用对象名.方法名调用Demo demo = new Demo();demo.de();//静态方法可以用对象名.方法名调用也可以用类名.方法名调用(推荐)Demo.ae();demo.ae();//在同一个类中可以直接调用ae();}
}
类的组成
代码块分类
普通块
{System.out.println("这是普通块");int num = 11;System.out.println(num);}
静态块
static {System.out.println("这是静态块");}
构造块
{System.out.println("这是构造块");}
代码块执行顺序
最先执行静态块,只在类加载的时候执行一次,所以一般创建工厂,数据库的初始化信息都写入静态块
然后执行构造块(不经常用)
再执行构造器
最后执行方法中的普通块
//对equals方法进行重写public boolean equals(Object obj){//将obj转为Miph类型Miph other = (Miph) obj;if (this.getBrand() == other.getBrand() && this.getPice() == other.getPice() && this.getYear() == other.getYear()){return true;}return false;}
public boolean equals(Object obj){if (obj instanceof Miph) {//将obj转为Miph类型Miph other = (Miph) obj;if (this.getBrand() == other.getBrand() && this.getPice() == other.getPice() && this.getYear() == other.getYear()) {return true;}}return false;}
public class Demo {Pig p = new Pig();Animal an = p;//向上转型an.shout();//将Animal转为pigPig pig = (Pig)an;//向下转型pig.eat();pig.age = 13;pig.weiht = 66;}
成员内部类有属性,方法,构造器等
修饰符:private,default,protect,public,final,abstract
内部类可以访问外部类的内容
静态内部类只能访问外部类中被static修饰的内容
外部类需要先创建内部类对象才能访问内部类的内容
内部类和外部类属性重名时id调用方法
System.out.println(age);System.out.println(this.age);System.out.println(Teston.this.age);
Teston.W w = new Teston.W();
Teston t = new Teston();
Teston.F f = t.new F();
public Comparable maethc() {class B implements Comparable{@Overridepublic int compareTo(Object o){return 100;}}return new B();}public Comparable maethc2() {//匿名内部类return new Comparable(){@Overridepublic int compareTo(Object o){return 200;}};
}