九、关键字:this的使用

  1. package com.atguigu.java2;
  2. /*
  3. * this关键字的使用
  4. * 1. this可以用来修饰、调用:属性、方法、构造器
  5. *
  6. * 2. this修饰属性和方法:
  7. * this理解为:当前对象 或 当前正在创建的对象
  8. *
  9. * 2.1 在类的方法当中,我们可以使用"this.属性"或"this.方法"的方式,调用当前对象的属性或方法
  10. * 但是,通常情况下,选择省略"this."。特殊情况下,如果方法的形参和属性同名,我们必须显示使用
  11. * "this.变量"的方式,表明此变量是属性,而非形参
  12. *
  13. * 2.2 在类的构造器当中,我们可以使用"this.属性"或"this.方法"的方式,调用当前对象的属性或方法
  14. * 但是,通常情况下,选择省略"this."。特殊情况下,如果构造器的形参和属性同名,我们必须显示使用
  15. * "this.变量"的方式,表明此变量是属性,而非形参
  16. *
  17. * 3. this调用构造器
  18. * 3.1 我们在类的构造器中,可以显示的使用"this(形参列表)"方式,调用本类中指定的其他构造器
  19. * 3.2 构造器中不能通过"this(形参列表)"方式,调用自己
  20. * 3.3 如果一个类中有n个构造器,则最多有n-1构造器中使用"this(形参列表)"
  21. * 3.4 规定:"this(形参列表)"必须在当前构造器首行
  22. * 3.5 构造器内部,最多只能声明一个"this(形参列表)",用来调用其他的构造器
  23. */
  24. public class PersonTest {
  25. public static void main(String[] args) {
  26. Person p1 = new Person();
  27. p1.setAge(1);
  28. System.out.println(p1.getAge());
  29. p1.eat();
  30. System.out.println();
  31. Person p2 = new Person("Jerrmy",20);
  32. }
  33. }
  34. class Person{
  35. private String name;
  36. private int age;
  37. public Person(){
  38. //this.eat();
  39. String info = "Person初始化时,需要考虑如下1,2,3,4(共40行代码)";
  40. System.out.println(info);
  41. }
  42. //构造器没有返回值
  43. public Person(String name){
  44. this();//调用空参构造器、且必须在当前构造器首行
  45. this.name = name;
  46. //Person初始化时,需要考虑如下1,2,3,4(共40行代码)
  47. }
  48. public Person(int age){
  49. this();
  50. this.age = age;
  51. //Person初始化时,需要考虑如下1,2,3,4(共40行代码)
  52. }
  53. public Person(String name,int age){
  54. this(age);
  55. this.name = name;
  56. //this.age = age;
  57. //Person初始化时,需要考虑如下1,2,3,4(共40行代码)
  58. }
  59. public void setName(String name){
  60. this.name = name;
  61. }
  62. public String getName(){
  63. return name;
  64. }
  65. public void setAge(int age){
  66. this.age = age;
  67. }
  68. public int getAge(){
  69. return age;
  70. }
  71. public void eat(){
  72. System.out.println("人吃饭");
  73. this.study();
  74. }
  75. public void study(){
  76. System.out.println("人学习");
  77. }
  78. }

9.1 练习11

  1. package com.atguigu.exer2;
  2. public class Boy {
  3. private String name;
  4. private int age;
  5. public Boy() {
  6. }
  7. public Boy(String name,int age){
  8. this.name = name;
  9. this.age = age;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. public void marry(Girl girl){
  24. System.out.println("我想娶" + girl.getName());
  25. }
  26. public void shout(){
  27. if(this.age >= 22){
  28. System.out.println("你可以登记结婚了");
  29. }else{
  30. System.out.println("多谈谈恋爱");
  31. }
  32. }
  33. }
  1. package com.atguigu.exer2;
  2. public class Girl {
  3. private String name;
  4. private int age;
  5. public Girl(){
  6. }
  7. public Girl(String name,int age){
  8. this.name = name;
  9. this.age = age;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public void marry(Boy boy){
  18. System.out.println("我想嫁给" + boy.getName());
  19. boy.marry(this);//传递的是Girl类型,this表示当前对象
  20. }
  21. public int compare(Girl girl){
  22. /*if(this.age > girl.age){
  23. return 1;
  24. }else if(this.age < girl.age){
  25. return -1;
  26. }else {
  27. return 0;
  28. }*/
  29. return this.age - girl.age;
  30. }
  31. }
  1. package com.atguigu.exer2;
  2. public class BoyGirlTest {
  3. public static void main(String[] args) {
  4. Boy boy = new Boy("罗密欧", 21);
  5. boy.shout();
  6. Girl girl = new Girl("朱丽叶", 18);
  7. girl.marry(boy);
  8. Girl girl1 = new Girl("祝英台", 18);
  9. int compare = girl.compare(girl1);
  10. if(compare > 0){
  11. System.out.println(girl.getName() + "大");
  12. }else if(compare > 0){
  13. System.out.println(girl1.getName() + "大");
  14. }else{
  15. System.out.println("一样大");
  16. }
  17. }
  18. }

9.2 实验1

9.3 实验2(难、收获大)

1.按照如下的 UML 类图,创建相应的类,提供必要的结构

4.(this、package、import) - 图1

在提款方法 withdraw()中,需要判断用户余额是否能够满足提款数额的要求,如果不能,

应给出提示。deposit()方法表示存款。

2.按照如下的 UML 类图,创建相应的类,提供必要的结构

4.(this、package、import) - 图2

  1. 按照如下的 UML 类图,创建相应的类,提供必要的结构
    4.(this、package、import) - 图3

    • addCustomer 方法必须依照参数(姓,名)构造一个新的 Customer 对象,然后把
      它放到 customer 数组中。还必须把 numberOfCustomer 属性的值加 1。

    • getNumOfCustomers 方法返回 numberofCustomers 属性值。

    • getCustomer 方法返回与给出的 index 参数相关的客户。

  2. 创建 BankTest 类,进行测试。
    Account

  1. package com.atguigu.exer4;
  2. public class Account {
  3. private double balance;
  4. public Account(double init_balance){
  5. this.balance = init_balance;
  6. }
  7. public double getBalance(){
  8. return balance;
  9. }
  10. //存钱操作
  11. public void deposit(double amt){
  12. if(amt > 0){
  13. balance += amt;
  14. }
  15. }
  16. //取钱操作
  17. public void withdraw(double amt){
  18. if(balance >= amt){
  19. balance -= amt;
  20. System.out.println("取钱成功");
  21. }else{
  22. System.out.println("余额不足");
  23. }
  24. }
  25. }

Bank类

  1. package com.atguigu.exer4;
  2. public class Bank {
  3. private Customer[] customers;//存放多个数组
  4. private int numerOfCustomer;//记录客户个数
  5. public Bank(){
  6. customers = new Customer[10];
  7. }
  8. //添加客户
  9. public void addCustomer(String f,String l){
  10. Customer cust = new Customer(f,l);//new一个,创建对象
  11. //报空指针异常,数组没有初始化
  12. customers[numerOfCustomer++] = cust;//存放地址
  13. }
  14. //获取指定位置上客户
  15. public Customer getCustomer(int index) {
  16. if(index >= 0 && index <= numerOfCustomer){
  17. return customers[index];
  18. }
  19. return null;
  20. }
  21. public int getNumerOfCustomer() {
  22. return numerOfCustomer;
  23. }
  24. }

Customer类

  1. package com.atguigu.exer4;
  2. public class Customer {
  3. private String firstName;
  4. private String lastName;
  5. private Account account;
  6. public Customer(String f, String l){
  7. this.firstName = f;
  8. this.lastName = l;
  9. }
  10. public Account getAccount() {
  11. return account;
  12. }
  13. public void setAccount(Account account) {
  14. this.account = account;
  15. }
  16. public String getFirstName() {
  17. return firstName;
  18. }
  19. public String getLastName() {
  20. return lastName;
  21. }
  22. }

BankTest类

  1. package com.atguigu.exer4;
  2. public class BankTest {
  3. public static void main(String[] args) {
  4. Bank bank = new Bank();
  5. bank.addCustomer("Jane","Smith");
  6. //匿名对象
  7. bank.getCustomer(0).setAccount(new Account(2000));
  8. bank.getCustomer(0).getAccount().withdraw(500);
  9. double balance = bank.getCustomer(0).getAccount().getBalance();
  10. System.out.println("客户 :" + bank.getCustomer(0).getFirstName() +
  11. "账户的余额为:" + balance);
  12. System.out.println("***************************");
  13. bank.addCustomer("万里", "鹏程");
  14. System.out.println("银行客户个数为 " + bank.getNumerOfCustomer());
  15. }
  16. }

十、关键字:package、 import的使用

  1. package com.atguigu.java2;
  2. //import的落脚点是类或接口
  3. import java.lang.reflect.Field;
  4. import java.util.*;
  5. import com.atguigu.exer4.Account;
  6. import com.atguigu.java2.java3.Dog;
  7. //import static的落脚点是类中的结构
  8. import static java.lang.System.*;
  9. import static java.lang.Math.*;
  10. /*import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.HashMap;*/
  13. /*
  14. * 一、package关键字的使用
  15. * 1.为了更好的实现项目中类的管理,提供包的概念
  16. * 2.使用package声明类或接口所属的包,声明在源文件的首行
  17. * 3.包,属于标识符,遵循标识符的命名规则、规范(xxxyyyzzz)、“见名知意”
  18. * 4.每“.”一次,就代表一层文件目录。
  19. *
  20. * 补充:同一个包下,不能命名同名的接口、类
  21. * 不同包下,可以命名同名的接口、类
  22. *
  23. * 二、import关键字的使用
  24. * import:导入
  25. * 1.在源文件中显示的使用import结构导入指定包下的类、接口
  26. * 2.声明在包的声明和类的声明之间
  27. * 3.如果需要导入多个结构,则并列写出即可
  28. * 4.可以使用"xxx.*"的方式,表示可以导入xxx包下的所有结构
  29. * 5.如果使用的是类或接口是java.lang包下定义的,则可以省略import结构
  30. * 6.如果使用的类或接口是本包下定义的,则可以省略import结构
  31. * 7.如果在源文件中,使用不同包下的同名的类则必须至少有一个类需要以全类名的方式显示
  32. * 8.使用"xxx.*"方式表明可以调用xxx包下的所有结构。但如果使用的是xxx子包下的结构,则
  33. * 仍需要显示调用,"xxx.*"指表示xxx包下的类或接口,不表示子包
  34. *
  35. * 9.import static:导入指定类或接口中的静态结构:属性或方法
  36. */
  37. public class PackageImportTest {
  38. public static void main(String[] args) {
  39. String info = Arrays.toString(new int[]{1,2,3});
  40. ArrayList list = new ArrayList();
  41. HashMap map = new HashMap();
  42. Scanner s = null;
  43. System.out.println("hello");
  44. Account acct = new Account(1000);
  45. //全类名的方式显示
  46. //com.atguigu.exer3.Account acct1 = new com.atguigu.exer3.Account(1000,2000,0.0123);
  47. //要导入,应为是com.atguigu.java2的子包下的结构
  48. Dog dog = new Dog();
  49. //lang包不用导,但是lang包下的子包仍然需要导包,和上一行一样
  50. Field field = null;
  51. //导入import static java.lang.System.*;
  52. out.println();
  53. //import static java.lang.Math.*;
  54. //long num = Math.round(123.45);
  55. long num = round(123.45);
  56. }
  57. }

第239-257 262 263 268 269还没看,先学继承从258