No_16_0226 Java基础学习第七天

文档版本 开发工具 测试平台 工程名字 日期 作者 备注
V1.0 2016.02.26 lutianfei none


成员变量与局部变量

  • 成员变量与局部变量区别:
    • A:在类中的位置不同
      • 成员变量:在类中,方法外
      • 局部变量:在方法定义中或者方法声明上。
    • B:在内存中的位置不同:
      • 成员变量:在堆内存
      • 局部变量:在栈内存
    • C:生命周期不同
      • 成员变量:随着对象的创建而存在,随着对象的消失而消失
      • 局部变量:随着方法的调用而存在,随着方法的调用完毕而存在
    • D:初始化值不同
      • 成员变量:有默认初始值
      • 局部变量:没有默认初始值,必须定义,赋值,然后才能使用。
  • 注:局部变量名称可以和成员变量名称一致,在方法中使用的时候,采用就近原则。

形式参数问题

  • 一个方法的形式参数是一个类类型(引用类型),这里其实需要的是该类的对象。
  1. //形式参数是 基本类型
  2. class Demo {
  3. public int sum(int a,int b) {
  4. return a + b;
  5. }
  6. }
  7. //形式参数是 引用类型
  8. class Student {
  9. public void show() {
  10. System.out.println("我爱学习");
  11. }
  12. }
  13. class StudentDemo {
  14. //如果你看到了一个方法的形式参数是一个类类型(引用类型),这里其实需要的是该类的对象。
  15. public void method(Student s) {
  16. //调用的时候,把main方法中的s的地址传递到了这里 Student s = new Student();
  17. s.show();
  18. }
  19. }
  20. class ArgsTest {
  21. public static void main(String[] args) {
  22. //形式参数是基本类型的调用
  23. Demo d = new Demo();
  24. int result = d.sum(10,20);
  25. System.out.println("result:"+result);
  26. System.out.println("--------------");
  27. //形式参数是引用类型的调用
  28. //需求:我要调用StudentDemo类中的method()方法
  29. StudentDemo sd = new StudentDemo();
  30. //创建学生对象
  31. Student s = new Student();
  32. sd.method(s); //把s的地址给到了这里
  33. }
  34. }

匿名对象

  • 匿名对象:就是没有名字的对象。
    • 是对象的一种简化表示形式
  • 匿名对象的两种使用情况
    • 对象调用方法仅仅一次的时候
    • 作为实际参数传递
  • 匿名调用好处
    • 匿名对象调用完毕就是垃圾。可以被垃圾回收器回收。
  1. class Student {
  2. public void show() {
  3. System.out.println("我爱学习");
  4. }
  5. }
  6. class StudentDemo {
  7. public void method(Student s) {
  8. s.show();
  9. }
  10. }
  11. class NoNameDemo {
  12. public static void main(String[] args) {
  13. //带名字的调用
  14. Student s = new Student();
  15. s.show();
  16. s.show();
  17. System.out.println("--------------");
  18. //匿名对象
  19. //new Student();
  20. //匿名对象调用方法
  21. new Student().show();
  22. new Student().show(); //这里其实是重新创建了一个新的对象
  23. System.out.println("--------------");
  24. //匿名对象作为实际参数传递
  25. StudentDemo sd = new StudentDemo();
  26. //匿名对象
  27. sd.method(new Student());
  28. //再来一个
  29. new StudentDemo().method(new Student());
  30. }
  31. }


封装概述

  • 封装概述
    • 是指隐藏对象的属性和实现细节,仅对外提供公共访问方式。
  • 好处:
    • 隐藏实现细节,提供公共的访问方式
    • 提高了代码的复用性
    • 提高安全性。
  • 封装原则:
    • 将不需要对外提供的内容都隐藏起来。
    • 把属性隐藏,提供公共方法对其访问。
  • 注:
    • 测试类一般只创建对象,调用方法。

private关键字

  • private关键字:

    • 是一个权限修饰符,是封装的一种体现。
    • 可以修饰成员(成员变量和成员方法)
    • 被private修饰的成员只在本类中才能访问。
  • private最常见的应用:

    • 把成员变量用private修饰
    • 提供对应的getXxx()/setXxx()方法
    • 一个标准的案例的使用

this关键字

  • this:代表所在类的对象引用
  • 记住:
    • 方法被哪个对象调用,this就代表那个对象
  1. class Student {
  2. //姓名
  3. private String name;
  4. //年龄
  5. private int age;
  6. //姓名获取值
  7. public String getName() {
  8. return name;
  9. }
  10. //姓名设置值
  11. public void setName(String name) { //name = "林青霞";
  12. //Student.name = name;
  13. this.name = name;
  14. }
  15. //年龄获取值
  16. public int getAge() {
  17. return age;
  18. }
  19. //年龄赋值
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. }
  24. //测试类
  25. class StudentTest {
  26. public static void main(String[] args) {
  27. //创建学生对象
  28. Student s = new Student();
  29. //给成员变量赋值
  30. s.setName("林青霞");
  31. s.setAge(27);
  32. //获取数据
  33. System.out.println(s.getName()+"---"+s.getAge());
  34. }
  35. }

构造方法

  • 构造方法作用概述
    • 给对象的数据进行初始化
  • 构造方法格式
    • 方法名类名相同
    • 没有返回值类型,连void都没有
    • 没有具体的返回值。(可以有return;这一语句,表示方法到这里结束)
  • 构造方法注意事项
    • 如果你不提供构造方法,系统会给出默认无参构造方法
    • 如果你提供了构造方法,系统将不再提供默认的无参构造方法。必须自己给出。(建议永远自己给出无参构造方法!)
    • 构造方法也是可以重载
  1. class Student {
  2. private String name;
  3. private int age;
  4. public Student() {
  5. System.out.println("这是无参构造方法");
  6. }
  7. //构造方法的重载格式
  8. public Student(String name) {
  9. System.out.println("这是带一个String类型的构造方法");
  10. this.name = name;
  11. }
  12. public Student(int age) {
  13. System.out.println("这是带一个int类型的构造方法");
  14. this.age = age;
  15. }
  16. public Student(String name,int age) {
  17. System.out.println("这是一个带多个参数的构造方法");
  18. this.name = name;
  19. this.age = age;
  20. }
  21. public void show() {
  22. System.out.println(name+"---"+age);
  23. }
  24. }
  25. class ConstructDemo2 {
  26. public static void main(String[] args) {
  27. //创建对象
  28. Student s = new Student();
  29. s.show();
  30. System.out.println("-------------");
  31. //创建对象2
  32. Student s2 = new Student("林青霞");
  33. s2.show();
  34. System.out.println("-------------");
  35. //创建对象3
  36. Student s3 = new Student(27);
  37. s3.show();
  38. System.out.println("-------------");
  39. //创建对象4
  40. Student s4 = new Student("林青霞",27);
  41. s4.show();
  42. }
  43. }


一个基本类的标准代码写法

    • 成员变量
    • 构造方法
      • 无参构造方法
      • 带参构造方法
    • 成员方法
      • getXxx()
      • setXxx()
  • 给成员变量赋值的方式
    • 无参构造方法+setXxx()
    • 带参构造方法
  1. /*
  2. 一个标准代码的最终版。
  3. 学生类:
  4. 成员变量:
  5. name,age
  6. 构造方法:
  7. 无参,带两个参
  8. 成员方法:
  9. getXxx()/setXxx()
  10. show():输出该类的所有成员变量值
  11. 给成员变量赋值:
  12. A:setXxx()方法
  13. B:构造方法
  14. 输出成员变量值的方式:
  15. A:通过getXxx()分别获取然后拼接
  16. B:通过调用show()方法搞定
  17. */
  18. class Student {
  19. //姓名
  20. private String name;
  21. //年龄
  22. private int age;
  23. //构造方法
  24. public Student() {
  25. }
  26. public Student(String name,int age) {
  27. this.name = name;
  28. this.age = age;
  29. }
  30. public String getName() {
  31. return name;
  32. }
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36. public int getAge() {
  37. return age;
  38. }
  39. public void setAge(int age) {
  40. this.age = age;
  41. }
  42. //输出所有的成员变量值
  43. public void show() {
  44. System.out.println(name+"---"+age);
  45. }
  46. }
  47. //测试类
  48. class StudentTest {
  49. public static void main(String[] args) {
  50. //方式1给成员变量赋值
  51. //无参构造+setXxx()
  52. Student s1 = new Student();
  53. s1.setName("林青霞");
  54. s1.setAge(27);
  55. //输出值
  56. System.out.println(s1.getName()+"---"+s1.getAge());
  57. s1.show();
  58. System.out.println("----------------------------");
  59. //方式2给成员变量赋值
  60. Student s2 = new Student("刘意",30);
  61. System.out.println(s2.getName()+"---"+s2.getAge());
  62. s2.show();
  63. }
  64. }


Student s = new Student();在内存中做了哪些事情?

  • 加载Student.class文件进内存
  • 内存为s开辟空间
  • 内存为学生对象开辟空间
  • 对学生对象的成员变量进行默认初始化
  • 对学生对象的成员变量进行显示初始化
  • 通过构造方法对学生对象的成员变量赋值
  • 学生对象初始化完毕,把对象地址赋值给s变量


static关键字

  • 可以修饰成员变量和成员方法
  • static关键字特点
    • 随着类的加载而加载
    • 优先于对象存在
    • 被类的所有对象共享
      • 这也是我们判断是否使用静态关键字的条件
    • 可以通过类名调用,也可以通过对象调用。
  • static关键字注意事项
    • 静态方法中是没有this关键字的(无法从静态上下文引用非静态 变量 xxx)
      • 因为静态时随着的加载而加载,this是随着对象的创建而存在。
      • 静态比对象先存在。
    • 静态方法只能访问静态的成员变量静态的成员方法,非静态方法可以随意访问。

  1. class Teacher {
  2. public int num = 10;
  3. public static int num2 = 20;
  4. public void show() {
  5. System.out.println(num); //隐含的告诉你访问的是成员变量
  6. System.out.println(this.num); //明确的告诉你访问的是成员变量
  7. System.out.println(num2);
  8. }
  9. public static void method() {
  10. //无法从静态上下文中引用非静态 变量 num
  11. //System.out.println(num);
  12. System.out.println(num2);
  13. //无法从静态上下文中引用非静态 方法 function()
  14. //function();
  15. function2();
  16. }
  17. public void function() {
  18. }
  19. public static void function2() {
  20. }
  21. }
  22. class TeacherDemo {
  23. public static void main(String[] args) {
  24. //创建对象
  25. Teacher t = new Teacher();
  26. t.show();
  27. System.out.println("------------");
  28. t.method();
  29. }
  30. }


静态变量成员变量的区别

  • 所属不同
    • 静态变量属于类,所以也称为为类变量
    • 成员变量属于对象,所以也称为实例变量(对象变量)
  • 内存中位置不同
    • 静态变量存储于方法区静态区
    • 成员变量存储于堆内存
  • 内存出现时间不同
    • 静态变量随着的加载而加载,随着类的消失而消失
    • 成员变量随着对象的创建而存在,随着对象的消失而消失
  • 调用不同
    • 静态变量可以通过类名调用,也可以通过对象调用
    • 成员变量只能通过对象名调用

main方法是静态的

  • public static void main(String[] args) {}
    • public 公共的,访问权限最大,因为被jvm调用,需要访问权限足够大。
    • static 静态的,不需要创建对象,被jvm调用,不用创建对象,直接类名访问
    • void被jvm调用,不需要给jvm返回值
    • main 一个通用的名称,虽然不是关键字,但是被jvm识别
    • String[] args 以前用于接收键盘录入的
      • eg:java MainDemo hello world java




posted @ 2016-02-28 17:10  鹿天斐  阅读(176)  评论(0编辑  收藏  举报