文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 1.类的实例化
- 2.this的引用
- 3.构造方法
- 4.默认初始化
- 5.就地初始化
- 6.static成员
- 8.代码块
- 9.内部类
1.类的实例化
class Dog { public int age; public String name; public void set(int age,String name) { this.age=age; this.name=name; } public void dayin() { System.out.println(this.age); System.out.println(this.name); } public void bark() { System.out.println("狗叫"); } public void eat() { System.out.println("狗吃"); } } public static void main1(String[] args) { Dog dog=new Dog(); //类的实例化 dog.age=3; dog.name="wang"; System.out.println(dog.age); System.out.println(dog.name); }
2.this的引用
我们一起来看一段代码
class Dog { public int age; public String name; public void set(int a,String b) { age=a; name=b; } public void dayin() { System.out.println(age); System.out.println(name); } public static void main2(String[] args) { Dog dog1=new Dog(); Dog dog2=new Dog(); dog1.set(3,"wang"); dog2.set(4,"li"); dog1.dayin(); dog2.dayin(); }
像这种情况如果我们把3,"wang",4,"li",参数传过去,set函数接受的时候不知道到底最后赋值给了dog1的age.name还是dog2的age和name,因此我们需要this,代码修改如下
class Dog { public int age; public String name; public void set(int a,String b) { this.age=a; //this可以认为当dog1传叁的时候,this相当于dog1,a b的值分别赋值给了dog1的age和name,dog2传叁的时候,同样的this就相当于dog2 this.name=b; // } public void dayin() { System.out.println(age); System.out.println(name); } public static void main2(String[] args) { Dog dog1=new Dog(); Dog dog2=new Dog(); dog1.set(3,"wang"); dog2.set(4,"li"); dog1.dayin(); dog2.dayin(); }
3.构造方法
构造方法是指方法名和类名相同的方法,当我们不去定义这个方法的时候,系统会默认定义这个方法。
class Student { public int age; public String name; public String grade; public void set(int age,String name,String grade) { this.age=age; this.name=name; this.grade=grade; } public void pint() { System.out.println(age+" "+name+" "+grade); } public void pint() { System.out.println(age+" "+name+" "+grade); } public Student() { //System.out.println("不带参数的构造方法"); this(7,"wang","java7"); //这里也可以通过this这个语句来给参数赋值,但是这个语句必须在第一行 pint(); } public Student(int age,String name,String grade) { System.out.println("带参数的构造方法"); this.age=age; this.name=name; this.grade=grade; pint(); } public static void main(String[] args) { Student student2=new Student(); }
4.默认初始化 是指即使我们没有给成员变量赋值时,但在对象空间被申请好了以后,对象中的成员变量已经设置好了成员变量
bite 0
char 'u0000’
short 0
int 0
long 0L
boolean false
float 0.0f
double 0.0f
String null
class Dog { public int age; public String name; public void dayin() { System.out.println(age); System.out.println(name); } } public static void main3(String[] args) { Dog dog3=new Dog(); System.out.println(dog3.age+" "+dog3.name); }
5.就地初始化 是指在声明成员变量时直接赋值
class Girl { int age=7; // String hobby="打篮球"; public void fun() { System.out.println("age="+age+" "+"hobby="+hobby); } } public static void main11(String[] args) { Girl girl=new Girl(); girl.fun(); }
6.static 成员
static 成员我们在访问的时候可以通过对象访问,也可以通过类名访问,最好通过类名访问
class Boy { public static int age=1; public static String name="lisi"; } public static void main15(String[] args) { Boy boy=new Boy(); System.out.println(boy.age); System.out.println(boy.name); System.out.println(Boy.age); System.out.println(Boy.name); }
class Boy { public static int age=1; public static String name="lisi"; // public static int age=1; // static String name="wang"; public static int fun() { return age; } } public static void main(String[] args) { Boy boy=new Boy(); System.out.println(boy.fun());//对象访问 System.out.println(Boy.fun());//类名访问 }
当我们不创建对象时也可以通过类名访问,与对象存不存在没有关系
class Boy { public static int age=1; public static String name="lisi"; } public static void main15(String[] args) { //Boy boy=new Boy(); // System.out.println(boy.age); // System.out.println(boy.name); System.out.println(Boy.age); System.out.println(Boy.name); }
static修饰的成员方法中,不可以出现this、非静态的变量、非静态的成员方法
class Boy { public int age=1; public static String name="lisi"; public static int fun() { return age; //静态方法中不能引用非静态变量age } } class Boy { public int age=1; public static String name="lisi"; public static void set(int age,String name) { this.age=age; //静态方法中不可以引用非静态变量 this this.name=name; } } class Boy { public int age = 1; public static String name = "lisi"; public void gun() { System.out.println("打印"); } public static void dayin() { this.gun(); //静态方法中不可以引用非静态方法,因为非静态方法中有this参数 } }
8.代码块
主要分为静态代码块和实例代码块,静态代码块用来初始化静态变量而实例代码块用来初始化非静态代码块
class Monther { public int age; public int month; public static String name; { this.age=1; this.month=8; System.out.println("实例代码块"); } static { name="wang"; System.out.println("静态代码块"); } // static //{ //name="li"; //System.out.println("静态代码块"); //} public Monther() { System.out.println("不带参数的构造方法"); } } public static void main(String[] args) { Monther monther=new Monther(); }
根据我们所想打印的不带参数的构造方法,
然而打印出来的却是这样一个结果,那么这又是什么原因呢?这是因为静态代码块中的静态成员变量在类加载时已经 开辟空间并初始化,实例代码块在创建对象完成后就被执行了,所以三者执行的先后顺序分别是静态代码块、实例代码块、构造方法
静态代码块不管生成多少对象,只会执行一次
class Monther { public int age; public int month; public static String name; { this.age=1; this.month=8; System.out.println("实例代码块"); } static { name="wang"; System.out.println("静态代码块"); } // static //{ //name="li"; //System.out.println("静态代码块"); //} public Monther() { System.out.println("不带参数的构造方法"); } } public static void main(String[] args) { Monther monther1=new Monther(); Monther monther2=new Monther(); }
第二次我们可以看到并没有打印静态代码块,说明静态代码块只执行一次
当有多个静态代码块时,静态代码块按照先后顺序序执行
class Monther { public static int age; public int month; public static String name="li"; //1 只要是static修饰的变量我们都可以认为是一个代码块 { //3 age=1; System.out.println("实例代码块"); } static //2 { name="wang"; System.out.println("静态代码块"); } static //4 { age++; name="xiao"; System.out.println("也是静态代码块"); } public Monther() //5 { System.out.println("不带参数的构造方法"); } public static void main(String[] args) { Monther monther1=new Monther(); System.out.println(Monther.age); System.out.println(Monther.name); }
9.内部类
内部类简单来说就是一个类当中含有另一个类
1.如何获取实例内部类对象?需要创建外部的和内部的对象
class Outer { public int age1 = 3; public int age2 = 7; public int age3= 2 class Inter { public int age4 = 3; public int age5 = 4; public void dayin() { System.out.println(age1); //内部类当中可以访问所有外部类的成员变量 System.out.println(age2); System.out.println(age3); System.out.println(age4); System.out.println(age5); } } } public static void main(String[] args) { Outer outer=new Outer(); //创建外部和内部的对象 Outer.Inter inter=outer.new Inter(); inter.dayin(); }
在实例内部类当中不能有静态的成员量,如果想要使用 需要加上 final
class Outer { public int age1 = 1; public int age2 = 7; class Inter { public static int age3=8;//实例内部类当中不能有静态成员变量 public static final int age=8; //加上 final age成为了一个常量 }
如果实例内部类和外部类有相同的成员变量,优先访问内部的成员变量
class Outer { public int age1 = 1; public int age2 = 7; class Inter { public int age1 = 3; public int age3 = 4; public void dayin() { System.out.println(age1); } } public static void main3(String[] args) { Outer outer=new Outer(); Outer.Inter inter=outer.new Inter(); inter.dayin(); }
在本题中优先访问内部的age1,所以最终打印出来的结果应该是3.
如果非要访问外部的,我们可以采取两种方式
class Outer { public int age1 = 1; public int age2 = 7; class Inter { public int age1 = 3; public void dayin() { System.out.println(Outer.this.age1);//第一种方式直接利用类 Outer outer = new Outer();第二种方式创建一个引用变量 System.out.println(outer.age1); } }
实例内部类总结1:内部类方法当中可以直接访问外部类的成员变量
2.当内部类和外部当中出现相同的成员变量时优先访问自己的,如果实在想访问,就用 类名.this.同名成员来访问也可以创建一个外部对象,通过引用的方式来访问
3.外部类中,不能直接访问实例内部类成员,如果要访问需要先创建内部类的对象
静态内部类
静态内部类被static修饰的成员内部类:在静态类当中只能访问外部类中的静态成员,如果确实想访问,需要通过外部类对象的引用
class Outer { public static int age1=1; public static int age2=2; int age4=8; static class Inter { public static int age3=3; public static void dayin() { System.out.println(age1); System.out.println(age2); System.out.println(age3); Outer outer=new Outer(); //age4是外部类当中非静态成员需要通过外部类对象的引用 System.out.println(outer.age4); } } } public class text { public static void main14(String[] args) { Outer.Inter inter=new Outer.Inter();//创建静态内部类对象时,不需要先创建外部类对象 inter.dayin(); }