目录
一、类定义和使用
1.1类的定义格式
在java中定义类时需要用到class关键字,具体语法如下
// 创建类class ClassName {field ; // 字段 ( 属性 ) 或者 成员变量method ; // 行为 或者 成员方法}
class为定义类的关键字,ClassName为类的名字,{}中为类的主体。
定义一个洗衣机的类:
class WashMachine {//成员属性/字段 定义在类的里面 方法的外面public String brand ; // 品牌public String type ; // 型号public double weight ; // 重量public double length ; // 长public double width ; // 宽public double height ; // 高public String color ; // 颜色//行为/成员方法:public void washClothes (){ // 洗衣服System . out . println ( " 洗衣功能 " );}public void dryClothes (){ // 脱水System . out . println ( " 脱水功能 " );}public void setTime (){ // 定时System . out . println ( " 定时功能 " );}}



1.2类的实例化
用类类型创建对象的过程,称为类的实例化,在java中采用new关键字,配合类名来实例化对象。
class PetDog {public String name ; // 名字public String color ; // 颜色// 狗的属性public void barks () {System . out . println ( name + ": 旺旺旺 ~~~" );}// 狗的行为public void wag () {System . out . println ( name + ": 摇尾巴 ~~~" );}}public class Main {public static void main ( String [] args ) {PetDog dogh = new PetDog (); // 通过 new 实例化对象 dogh就是一个引用 指向了PetDog对象dogh . name = " 阿黄 " ;dogh . color = " 黑黄 " ;dogh . barks ();dogh . wag ();PetDog dogs = new PetDog ();dogs . name = " 阿黄 " ;dogs . color = " 黑黄 " ;dogs . barks ();dogs . wag ();}}
注意事项
对象的成员属性在没有赋值的时候,引用类型为null,基本类型为对应的零型
二、this引用
public void setDay ( int year , int month , int day ){year = year ;month = month ;day = day ;}
解决方法:
1.变量名不一样
2.通过this引用(变量名一不一样都推荐用)
public void setDay ( int year , int month , int day ){this.year = year ;this.month = month ;this.day = day ;}
2.在类中方法调用其他方法
public void eat(){
System.out.println(this.name + ": 摇尾巴~~~");
this.show();
}
public void show(){
System.out.println(this.name + ": 哈哈哈~~~");
}
三、对象的构造及初始化
构造方法
public class Date {public int year ;public int month ;public int day ;// 构造方法:// 名字与类名相同,没有返回值类型,设置为void也不行// 一般情况下使用public修饰// 在创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次public Date ( int year , int month , int day ){this . year = year ;this . month = month ;this . day = day ;System . out . println ( "Date(int,int,int) 方法被调用了 " );}public void printDate (){System . out . println ( year + "-" + month + "-" + day );}public static void main ( String [] args ) {// 此处创建了一个 Date 类型的对象,并没有显式调用构造方法Date d = new Date ( 2021 , 6 , 9 ); // 输出 Date(int,int,int) 方法被调用了d . printDate (); // 2021-6-9}}
注意:构造方法的作用就是对对象中的成员进行初始化,并不负责给对象开辟空间。
当调用完成构造方法之后 对象才产生了
public class Date {public int year ;public int month ;public int day ;// 无参构造方法public Date (){this . year = 1900 ;this . month = 1 ;this . day = 1 ;}// 带有三个参数的构造方法public Date ( int year , int month , int day ) {this . year = year ;this . month = month ;this . day = day ;}public void printDate (){System . out . println ( year + "-" + month + "-" + day );}public static void main ( String [] args ) {Date d = new Date ();d . printDate ();}}
如果用户没有显式定义,编译器会生成一份默认的构造方法,生成的默认构造方法一定是无参的
public class Date {public int year ;public int month ;public int day ;public void printDate (){System . out . println ( year + "-" + month + "-" + day );}public static void main ( String [] args ) {Date d = new Date ();d . printDate ();}}
上述Date类中,没有定义任何构造方法,编译器会默认生成一个不带参数的构造方法
注意:一旦用户定义,编译器则不再生成。
构造方法中,可以通过this调用其他构造方法来简化代码
public class Date {public int year ;public int month ;public int day ;// 无参构造方法 -- 内部给各个成员赋值初始值,该部分功能与三个参数的构造方法重复// 此处可以在无参构造方法中通过this调用带有三个参数的构造方法(前提是得有其他的构造方法)// 但是this(1900,1,1);必须是构造方法中第一条语句public Date (){//System.out.println(year); 注释取消掉,编译会失败this ( 1900 , 1 , 1 );//this.year = 1900;//this.month = 1;//this.day = 1;}// 带有三个参数的构造方法public Date ( int year , int month , int day ) {this . year = year ;this . month = month ;this . day = day ;}}
四、封装


///快捷键///
封装扩展之包
(包名内所有英文字母必须是小写)
自定义包:


五、static成员
5.1static修饰成员变量
public class Student {public String name ;public String gender ;public int age ;public double score ;public static String classRoom = "306" ;// ...public static void main ( String [] args ) {// 静态成员变量可以直接通过类名访问System . out . println ( Student . classRoom );Student s1 = new Student ( "Li leilei" , " 男 " , 18 , 3.8 );Student s2 = new Student ( "Han MeiMei" , " 女 " , 19 , 4.0 );Student s3 = new Student ( "Jim" , " 男 " , 18 , 2.6 );// 也可以通过对象访问:但是 classRoom 是三个对象共享的System . out . println ( s1 . classRoom );System . out . println ( s2 . classRoom );System . out . println ( s3 . classRoom );
5.2static修饰成员方法
public static String getClassRoom (){System . out . println ( this );return classRoom ;}// 编译失败: Error:(35, 28) java: 无法从静态上下文中引用非静态 变量 thispublic static String getClassRoom (){age += 1 ;return classRoom ;}// 编译失败: Error:(35, 9) java: 无法从静态上下文中引用非静态 变量 age
4. 静态方法中不能调用任何非静态方法,因为非静态方法有this参数,在静态方法中调用时候无法传递this引用
public static String getClassRoom (){doClass ();return classRoom ;}// 编译报错: Error:(35, 9) java: 无法从静态上下文中引用非静态 方法 doClass()
5. 静态方法无法重写,不能用来实现多态
5.3static成员变量初始化
public class Student {private String name ;private String gender ;private int age ;private double score ;private static String classRoom = "306" ;// ...}
2. 静态代码块初始化
六、代码块
6.1普通代码块
6.2构造代码块(比构造代码先执行)
public class Student {// 实例成员变量private String name ;private String gender ;private int age ;private double score ;public Student () {System . out . println ( "I am Student init()!" );}// 实例代码块{this . name = "张三" ;this . age = 12 ;this . sex = "man" ;System . out . println ( "I am instance init()!" );}public void show (){System . out . println ( "name: " + name + " age: " + age + " sex: " + sex );}}public class Main {public static void main ( String [] args ) {Student stu = new Student ();stu . show ();}}
6.3静态代码块
使用static定义的代码块称为静态代码块。一般用于初始化静态成员变量。
public class Student {private String name ;private String gender ;private int age ;private double score ;private static String classRoom ;// 实例代码块{this . name = "bit" ;this . age = 12 ;this . gender = "man" ;System . out . println ( "I am instance init()!" );}// 静态代码块static {classRoom = "bit306" ;System . out . println ( "I am static init()!" );}public Student (){System . out . println ( "I am Student init()!" );}public static void main ( String [] args ) {Student s1 = new Student ();Student s2 = new Student ();}}
顺序:静态->实例->构造方法!!!!
七、对象的打印
如果想要默认打印对象中的属性该如何处理呢?答案:重写toString方法即可。