类的封装

封装:将将对象的状态信息隐藏在对象内部,不允许外部程序直接访问对象的内部信息,而是通过该类提供的方法实现对内部信息的操作访问

实现封装:private关键字修饰类的属性,让类的属性私有化,被私有化的属性只能在类中访问

  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. private int price = 3;
  5. public String getBrand() {
  6. return brand;
  7. }
  8. public void setBrand(String brand){
  9. this.brand = brand;
  10. }
  11. public int getNum() {
  12. return num;
  13. }
  14. public void setNum(int num){
  15. if(num <= 0){
  16. system.out.println("您输入的数量有误");
  17. } else {
  18. this.num = num;
  19. }
  20. }
  21. public void read() {
  22. int total_price = num * price;
  23. System.out.println("The price of" + num + brand + " is " + total_price + " yuan.");
  24. }
  25. }

构造方法

构造方法用于在实例化对象时(注意不是声明对象时)为对象的属性赋值,不定义构造方法即会创建默认构造方法。

语法:

  • (1)构造方法的名称必须与类名一致
  • (2)构造方法名称前不能有任何返回值类型的声明
  • (3)不能在构造方法中使用 return 返回一个值,但是可以单独写 return 语句作为方法的结束
  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. private int price = 3;
  5. public Cookie(String b, int n) {
  6. brand = b;
  7. num = n;
  8. }
  9. public void read() {
  10. int total_price = num * price;
  11. System.out.println("The price of" + num + brand + " is " + total_price + " yuan.");
  12. }
  13. }

构造方法和普通方法一样可以重载

  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. private int price = 3;
  5. public Cookie() { //自定义无参构造方法防止new对象时出错
  6. }
  7. public Cookie(String brand, int num) {
  8. this.brand = brand;
  9. this.num = num;
  10. }
  11. public Cookie(String b) {
  12. brand = b;
  13. }
  14. public Cookie(int n) {
  15. num = n;
  16. }
  17. public void setBrand(String brand){
  18. this.brand = brand;
  19. }
  20. public void setNum(int num){
  21. if(num <= 0){
  22. system.out.println("您输入的数量有误");
  23. } else {
  24. this.num = num;
  25. }
  26. }
  27. public void read() {
  28. int total_price = num * price;
  29. System.out.println("The price of" + num + brand + " is " + total_price + " yuan.");
  30. }
  31. }

构造块

普通代码块就是直接在方法或是语句中定义的代码块;

构造代码块是直接在类中定义的代码块:

  • (1)实例化对象时,构造块的执行顺序大于构造方法,执行顺序和构造块写在前面还是后面没有关系;
  • (2)每实例一个对象时都会在执行构造方法之前执行构造代码块;
  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. private int price = 3;
  5. {
  6. System.out.println("Welcome to our shop!");
  7. }
  8. public Cookie() { //自定义无参构造方法防止new对象时出错
  9. }
  10. public Cookie(String brand, int num) {
  11. this.brand = brand;
  12. this.num = num;
  13. }
  14. public void read() {
  15. int total_price = num * price;
  16. System.out.println("The price of" + num + brand + " is " + total_price + " yuan.");
  17. }
  18. }

关键字

this关键字

任何方法都具有一个隐式 ( implicit ) 参数, 是出现在方法名前的对象,关键字this表示隐式参数。

使用this关键字调用本类中的属性

构造方法中参数如果和成员变量相同,则会导致成员变量和局部变量名称冲突,编译器无法确定哪个名称是当前对象的属性。

  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. public Cookie(String brand, int num) {
  5. brand = brand;
  6. num = num;
  7. } //error,赋值无法成功
  8. }

可以将构造方法里的参数换个名字,但是这样容易降低程序可读性

  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. public Cookie(String b, int n) {
  5. brand = b;
  6. num = n;
  7. }
  8. }

也可以使用this关键字

  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. public Cookie(String brand, int num) {
  5. this.brand = brand; //使用this关键字
  6. this.num = num;
  7. }
  8. }

使用this关键字调用成员方法

  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. private int price = 3;
  5. public void read() {
  6. int total_price = num * price;
  7. System.out.println("The price of" + num + brand + " is " + total_price + " yuan.");
  8. }
  9. public void tribute() {
  10. this.read();
  11. System.out.println("Thank you for your purchase.");
  12. }
  13. }

使用this关键字调用构造方法

构造方法是在实例化对象时被 Java 虚拟机自动调用,在程序中不能像调用其他成员方法一样调用构造方法,但可以在一个构造方法中使用“this(参数 1,参数 2…)”的形式调用其他的构造方法

  • (1)只能在构造方法中使用 this 调用其他的构造方法,不能在成员方法中通过 this 调用其他构造方法
  • (2)在构造方法中,使用 this 调用构造方法的语句必须位于第一行,且只能出现一次
  • (3)不能在一个类的两个构造方法中使用 this 互相调用

这样,只需对公共的构造器代码部分只编写一次即可。

  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. private int price = 3;
  5. public Cookie() { //自定义无参构造方法防止new对象时出错
  6. }
  7. public Cookie(String brand, int num) {
  8. this(); //调用无参的构造方法
  9. this.brand = brand;
  10. this.num = num;
  11. }
  12. public void setBrand(String brand){
  13. this.brand = brand;
  14. }
  15. public void setNum(int num){
  16. if(num <= 0){
  17. system.out.println("您输入的数量有误");
  18. } else {
  19. this.num = num;
  20. }
  21. }
  22. public void read() {
  23. int total_price = num * price;
  24. System.out.println("The price of" + num + brand + " is " + total_price + " yuan.");
  25. }
  26. }

在Java中,this引用等价于C的this指针。但是在 C中, 一个构造器不能调用另一个构造器,在C++中,必须将抽取出的公共初始化代码编写成一个独立的方法。

static关键字

用来修饰属于类且不属于类对象的变量和函数。

静态属性

static修饰的属性是静态属性(全局属性),被所有对象共享。static只能修饰成员变量,不能修饰局部变量。

静态属性可以使用类名直接访问:类名.属性名,也可以用实例对象名访问:实例对象名.属性名

非静态的属性保存在堆内存中,被其对象专有;静态属性只会分配一块内存空间,被类所有对象共享。

  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. static int price = 3;
  5. /*price是静态属性,被Cookie类的所有对象共享,只要对某个对象修改price,全部对象的price均改变*/
  6. public void read() {
  7. int total_price = num * price;
  8. System.out.println("The price of" + num + brand + " is " + total_price + " yuan.");
  9. }
  10. }

静态方法

static修饰的方法是静态方法,静态方法不能调用默认的this指针,在不创建对象的情况下,通过类名就可以调用静态方法:类名.方法,也可以通过对象访问:实例对象名.方法。

静态方法是一种不能向对象实施操作的方法 。

  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. static int price = 3; //price是静态属性
  5. public void read() {
  6. int total_price = num * price;
  7. System.out.println("The price of" + num + brand + " is " + total_price + " yuan.");
  8. }
  9. public static void setPrice(price) { //setPrice是静态方法,用于修改静态属性price
  10. Cookie.price = price;
  11. }
  12. public Cookie() { //自定义无参构造方法防止new对象时出错
  13. }
  14. public Cookie(String brand, int num) {
  15. this(); //调用无参的构造方法
  16. this.brand = brand;
  17. this.num = num;
  18. }
  19. public void setBrand(String brand){
  20. this.brand = brand;
  21. }
  22. public void setNum(int num){
  23. if(num <= 0){
  24. system.out.println("您输入的数量有误");
  25. } else {
  26. this.num = num;
  27. }
  28. }
  29. }

静态代码块

static修饰的代码块是静态代码块,在类被加载时执行,由于类只加载一次,则静态代码块也只执行一次

  1. class Cookie{
  2. private String brand;
  3. private int num;
  4. private int price = 3;
  5. static {
  6. System.out.println("Welcome to our shop!");
  7. }
  8. public Cookie() {
  9. }
  10. public Cookie(String brand, int num) {
  11. this.brand = brand;
  12. this.num = num;
  13. }
  14. public void read() {
  15. int total_price = num * price;
  16. System.out.println("The price of" + num + brand + " is " + total_price + " yuan.");
  17. }
  18. }

在程序中通常使用静态代码块对类的成员变量进行初始化。

包

在Java中,package与import语句类似于C++中的namespace和using指令。