// 创建类class ClassName{fifield ; // 字段 ( 属性 ) 或者 成员变量method ; // 行为 或者 成员方法}
class WashMachine {public String brand ; // 品牌public String type ; // 型号public double weight ; // 重量public double lenght ; // 长public double weidth ; // 宽public double height ; // 高public String color ; // 颜色public void WashClothes (){ // 洗衣服System . out . println ( " 洗衣功能 " );}public void dryClothes (){ // 脱水System . out . println ( " 脱水功能 " );}public void SetTime (){ // 定时System . out . println ( " 定时功能 " );}}

- public class Main
- {
- public static void main(String[] args)
- {
- PetDog dogh = new PetDog(); //通过new实例化对象
- dogh.name = "阿黄";
- dogh.color = "黑黄";
- dogh.barks();
- dogh.wag();
- PetDog dogs = new PetDog();
- dogs.name = "阿黄";
- dogs.color = "黑黄";
- dogs.barks();
- dogs.wag();
- }
- }
输出结果: 阿黄: 旺旺旺~~~ 阿黄: 摇尾巴~~~ 赛虎: 旺旺旺~~~ 赛虎: 摇尾巴~~~

- public class Date
- {
- public int year;
- public int month;
- public int day;
- public void setDay(int y, int m, int d)
- {
- year = y;
- month = m;
- day = d;
- }
- public void printDate()
- {
- System.out.println(year + "/" + month + "/" + day);
- }
- public static void main(String[] args)
- {
- // 构造三个日期类型的对象 d1 d2 d3
- Date d1 = new Date();
- Date d2 = new Date();
- Date d3 = new Date();
- // 对d1,d2,d3的日期设置
- d1.setDay(2020,9,15);
- d2.setDay(2020,9,16);
- d3.setDay(2020,9,17);
- // 打印日期中的内容
- d1.printDate();
- d2.printDate();
- d3.printDate();
- }
- }
public void setDay ( int year , int month , int day ){year = year ;month = month ;day = day ;}

- public class Date
- {
- public int year;
- public int month;
- public int day;
- public void setDay(int year, int month, int day)
- {
- this.year = year;
- this.month = month;
- this.day = day;
- }
- public void printDate()
- {
- System.out.println(this.year + "/" + this.month + "/" + this.day);
- }
- }
- public static void main(String[] args)
- {
- Date d = new Date();
- d.setDay(2020,9,15);
- d.printDate();
- }

public static void main ( String [] args ) {int a ;System . out . println ( a );}// Error:(26, 28) java: 可能尚未初始化变量 a
public static void main ( String [] args ) {Date d = new Date ();d . printDate ();d . setDate ( 2021 , 6 , 9 );d . printDate ();}// 代码可以正常通过编译
- 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();
- }
- }
- public class Date
- {
- public int year;
- public int month;
- public int day;
- 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();
- }
- }
-
- /*
- Error:(26, 18) java: 无法将类 extend01.Date中的构造器 Date应用到给定类型;
- 需要: int,int,int
- 找到: 没有参数
- 原因: 实际参数列表和形式参数列表长度不同
- */
- 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;
- }
- }
- public Date()
- {
- this(1900,1,1);
- }
- public Date(int year, int month, int day)
- {
- this();
- }
- /*
- 无参构造器调用三个参数的构造器,而三个参数构造器有调用无参的构造器,形成构造器的递归调用 编译报错:Error:(19, 12) java: 递归构造器调用
- */
- public class Date
- {
- public int year;
- public int month;
- public int day;
- public Date(int year, int month, int day)
- {
- // 成员变量在定义时,并没有给初始值, 为什么就可以使用呢?
- System.out.println(this.year); System.out.println(this.month);
- System.out.println(this.day);
- }
- public static void main(String[] args)
- {
- // 此处a没有初始化,编译时报错:
- // Error:(24, 28) java: 可能尚未初始化变量a
- // int a;
- // System.out.println(a);
- Date d = new Date(2021,6,9);
- }
- }

要搞清楚这个过程,就需要知道 new 关键字背后所发生的一些事情:
Date d = new Date ( 2021 , 6 , 9 );

5. 设置对象头信息(关于对象内存模型后面会介绍)
- public class Date
- {
- public int year = 1900;
- public int month = 1;
- public int day = 1;
- public Date()
- {
-
- }
- public Date(int year, int month, int day)
- {
-
- }
- public static void main(String[] args)
- {
- Date d1 = new Date(2021,6,9);
- Date d2 = new Date();
- }
- }

比如:
- public class Computer
- {
- private String cpu; // cpu
- private String memory; // 内存
- public String screen; // 屏幕
- String brand; // 品牌---->default属性
-
- public Computer(String brand, String cpu, String memory, String screen)
- {
- this.brand = brand;
- this.cpu = cpu;
- this.memory = memory;
- this.screen = screen;
- }
-
- public void Boot()
- {
- System.out.println("开机~~~");
- }
-
- public void PowerOff()
- {
- System.out.println("关机~~~");
- }
-
- public void SurfInternet()
- {
- System.out.println("上网~~~");
- }
- }
-
- public class TestComputer
- {
- public static void main(String[] args)
- {
- Computer p = new Computer("HW", "i7", "8G", "13*14");
- System.out.println(p.brand); // default属性:只能被本包中类访问
- System.out.println(p.screen);// public属性: 可以任何其他类访问
- //System.out.println(p.cpu);// private属性:只能在Computer类中访问,不能被其他类访问
- }
- }
- public class Test
- {
- public static void main(String[] args)
- {
- java.util.Date date = new java.util.Date(); // 得到一个毫秒级别的时间戳
- System.out.println(date.getTime());
- }
- }
- import java.util.Date;
- public class Test
- {
- public static void main(String[] args)
- {
- Date date = new Date();
- // 得到一个毫秒级别的时间戳
- System.out.println(date.getTime());
- }
- }
- import java.util.*;
- public class Test
- {
- public static void main(String[] args)
- {
- Date date = new Date(); // 得到一个毫秒级别的时间戳
- System.out.println(date.getTime());
- }
- }
import java . util . * ;import java . sql . * ;public class Test {public static void main ( String [] args ) {// util 和 sql 中都存在一个 Date 这样的类 , 此时就会出现歧义 , 编译出错Date date = new Date ();System . out . println ( date . getTime ());}}// 编译出错Error :( 5 , 9 ) java : 对 Date 的引用不明确java . sql 中的类 java . sql . Date 和 java . util 中的类 java . util . Date 都匹配
- import java.util.*; import java.sql.*;
- public class Test
- {
- public static void main(String[] args)
- {
- java.util.Date date = new java.util.Date();
- System.out.println(date.getTime());
- }
- }
- import static java.lang.Math.*;
- public class Test
- {
- public static void main(String[] args)
- {
- double x = 30;
- double y = 40;
- // 静态导入的方式写起来更方便一些.
- // double result = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
- double result = sqrt(pow(x, 2) + pow(y, 2));
- System.out.println(result);
- }
- }

2. 在弹出的对话框中输入包名, 例如 com.bit.demo1

3. 在包中创建类, 右键包名 -> 新建 -> 类, 然后输入类名即可

4. 此时可以看到我们的磁盘上的目录结构已经被 IDEA 自动创建出来了

5. 同时我们也看到了, 在新创建的 Test.java 文件的最上方, 就出现了一个 package 语句

6.3.4 包的访问权限控制举例
- package com.bit.demo1;
- public class Computer
- {
- private String cpu; // cpu
- private String memory; // 内存
- public String screen; // 屏幕
- String brand; // 品牌
- public Computer(String brand, String cpu, String memory, String screen)
- {
- this.brand = brand;
- this.cpu = cpu;
- this.memory = memory;
- this.screen = screen;
- }
- public void Boot()
- {
- System.out.println("开机~~~");
- }
- public void PowerOff()
- {
- System.out.println("关机~~~");
- }
- public void SurfInternet()
- {
- System.out.println("上网~~~");
- }
- }
- ///
- package com.bite.demo2;
- import com.bite.demo1.Computer;
- public class TestComputer
- {
- public static void main(String[] args)
- {
- Computer p = new Computer("HW", "i7", "8G", "13*14");
- System.out.println(p.screen);
- // System.out.println(p.cpu); // 报错:cup是私有的,不允许被其他类访问
- // System.out.println(p.brand); // 报错:brand是default,不允许被其他包中的类访问
- }
- }
- // 注意:如果去掉Computer类之前的public修饰符,代码也会编译失败
6.3.5 常见的包
public class Student {// ...public static void main ( String [] args ) {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 );}}

假设三个同学是同一个班的,那么他们上课肯定是在同一个教室,那既然在同一个教室,那能否给类中再加一个成员变量,来保存同学上课时的教室呢?答案是不行的。
public class Student {public String name ;public String gender ;public int age ;public double score ;public static String classRoom = "Bit306" ;// ...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 );}}
public class Student {private String name ;private String gender ;private int age ;private double score ;private static String classRoom = "Bit306" ;// ...}public class TestStudent {public static void main ( String [] args ) {System . out . println ( Student . classRoom );}}编译失败:Error :( 10 , 35 ) java : classRoom 在 extend01 . Student 中是 private 访问控制
public class Student {// ...private static String classRoom = "Bit306" ;// ...public static String getClassRoom (){return classRoom ;}}public class TestStudent {public static void main ( String [] args ) {System . out . println ( Student . getClassRoom ());}}输出: Bit306
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
public static String getClassRoom (){doClass ();return classRoom ;}// 编译报错: Error:(35, 9) java: 无法从静态上下文中引用非静态 方法 doClass()
public class Student {private String name ;private String gender ;private int age ;private double score ;private static String classRoom = "Bit306" ;// ...}
- public class Main
- {
- public static void main(String[] args)
- {
- {
- //直接使用{}定义,普通方法块
- int x = 10 ;
- System.out.println("x1 = " +x);
- }
- int x = 100 ;
- System.out.println("x2 = " +x);
- }
- }
-
- // 执行结果 x1 = 10 x2 = 100
- 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 = "bit";
- 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();
- }
- }
-
- // 运行结果
- I am instance init()!
- I am Student init()!
- name: bit age: 12 sex: man
- 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();
- }
- }
public class OutClass {class InnerClass {}}// OutClass 是外部类// InnerClass 是内部类
public class A {}class B {}// A 和 B 是两个独立的类,彼此之前没有关系
public class OutClass {// 成员位置定义:未被 static 修饰 ---> 实例内部类public class InnerClass1 {}// 成员位置定义:被 static 修饰 ---> 静态内部类static class InnerClass2 {}public void method (){// 方法中也可以定义内部类 ---> 局部内部类:几乎不用class InnerClass5 {}}}
- public class OutClass
- {
- private int a; static int b; int c;
- public void methodA()
- {
- a = 10;
- System.out.println(a);
- }
- public static void methodB()
- {
- System.out.println(b);
- }
- // 实例内部类:未被static修饰
- class InnerClass
- {
- int c;
- public void methodInner()
- {
- // 在实例内部类中可以直接访问外部类中:任意访问限定符修饰的成员
- a = 100;
- b =200;
- methodA();
- methodB();
- // 如果外部类和实例内部类中具有相同名称成员时,优先访问的是内部类自己的
- c = 300;
- System.out.println(c);
- // 如果要访问外部类同名成员时候,必须:外部类名称.this.同名成员名字
- OutClass.this.c = 400;
- System.out.println(OutClass.this.c);
- }
- }
- public static void main(String[] args)
- {
- // 外部类:对象创建 以及 成员访问
- OutClass outClass = new OutClass();
- System.out.println(outClass.a);
- System.out.println(OutClass.b);
- System.out.println(outClass.c);
- outClass.methodA();
- outClass.methodB();
- System.out.println("=============实例内部类的访问=============");
- // 要访问实例内部类中成员,必须要创建实例内部类的对象
- // 而普通内部类定义与外部类成员定义位置相同,因此创建实例内部类对象时必须借助外部类
- // 创建实例内部类对象
- OutClass.InnerClass innerClass1 = new OutClass().new InnerClass();
- // 上述语法比较怪异,也可以先将外部类对象先创建出来,然后再创建实例内部类对象
- OutClass.InnerClass innerClass2 = outClass.new InnerClass();
- innerClass2.methodInner();
- }
- }
- public class OutClass
- {
- private int a; static int b;
- public void methodA()
- {
- a = 10;
- System.out.println(a);
- }
- public static void methodB()
- {
- System.out.println(b);
- }
- // 静态内部类:被static修饰的成员内部类
- static class InnerClass
- {
- public void methodInner()
- {
- // 在内部类中只能访问外部类的静态成员
- // a = 100; // 编译失败,因为a不是类成员变量
- b =200;
- // methodA(); // 编译失败,因为methodB()不是类成员方法
- methodB();
- }
- }
- public static void main(String[] args)
- {
- // 静态内部类对象创建 & 成员访问
- OutClass.InnerClass innerClass = new OutClass.InnerClass();
- innerClass.methodInner();
- }
- }
- public class OutClass
- {
- int a = 10;
- public void method()
- {
- int b = 10;
- // 局部内部类:定义在方法体内部
- // 不能被public、static等访问限定符修饰
- class InnerClass
- {
- public void methodInnerClass()
- {
- System.out.println(a);
- System.out.println(b);
- }
- }
- // 只能在该方法体内部使用,其他位置都不能用
- InnerClass innerClass = new InnerClass();
- innerClass.methodInnerClass();
- }
- public static void main(String[] args)
- {
- // OutClass.InnerClass innerClass = null; 编译失败
- }
- }
- public class Person
- {
- String name;
- String gender;
- int age;
- public Person(String name, String gender, int age)
- {
- this.name = name;
- this.gender = gender;
- this.age = age;
- }
- public static void main(String[] args
- {
- Person person = new Person("Jim","男", 18);
- System.out.println(person);
- }
- }
-
-
- // 打印结果:day20210829.Person@1b6d3586
- public class Person
- {
- String name;
- String gender;
- int age;
- public Person(String name, String gender, int age)
- {
- this.name = name;
- this.gender = gender;
- this.age = age;
- }
- @Override
- public String toString()
- {
- return "[" + name + "," + gender + "," + age + "]";
- }
- public static void main(String[] args)
- {
- Person person = new Person("Jim","男", 18);
- System.out.println(person);
- }
- }
-
- // 输出结果:[Jim,男,18]