Java面向对象2

  • 2022-01-08
  • Admin

一、this关键字
1.this的作用:
·this表示的是当前对象本身,
·更准确地说,this代表当前对象的一个引用。
2.普通方法中使用this
·区分类成员属性和方法的形参
·调用当前对象的其他方法(可以省略)
·位置:任意
3.构造方法中使用this
·使用this来调用其它构造方法
·位置:必须是第一条语句
4.this不能用于static方法。(讲完static,大家就知道为什么了!)
5.this测试代码

  1. ublic class TestThis {
  2. int a,b,c;
  3. TestThis(){
  4. System.out.println("正要new一个Hello对象");
  5. }
  6. TestThis(int a,int b){
  7. //Hello();// //这样是无法调用构造方法的!
  8. this(); //调用无参的构造方法,并且必须位于第一行!
  9. a = a;//这里都是指的局部变量而不是成员变量
  10. this.a = a;//这样就区分了成员变量和局部变量. 这种情况占了this使用情况的大多数!
  11. this.b = b;
  12. }
  13. TestThis(int a,int b,int c){
  14. this(a,b);//调用无参的构造方法,并且必须位于第一行!
  15. this.c = c;
  16. }
  17. void sing(){
  18. }
  19. void chifan(){
  20. this.sing();//sing();
  21. System.out.println("你妈妈喊你回家吃饭!");
  22. }
  23. public static void main(String[] args){
  24. TestThis hi = new TestThis(2,3);
  25. hi.chifan();
  26. }
  27. }

6. 课堂demo【重点掌握】

  1. package netclass02;
  2. /**
  3. * @Auther: Yu Panpan
  4. * @Date: 2022/1/7 - 01 - 07 - 14:24
  5. * @Description: netclass02
  6. * @version: 1.0
  7. */
  8. /*
  9. * this:表示当前对象的指针
  10. * 指向当前对象,表示当前对象的引用
  11. * 用途:
  12. * 1、构造方法,当构造方法中的参数名称跟类的成员变量名称一样的时候,可以使用this代表当前对象
  13. * 注意:有了this之后,就可以将构造方法的参数跟成员变量保持一致(符合命名规范)
  14. * 当构造方法中需要调用其他的构造方法时,可以使用this(参数列表)调用其他构造方法,但是必须位于方法体的第一行(规则)
  15. * 2、普通方法中:
  16. * 当多个普通方法之间需要相互调用的时候,可以使用this来进行调用,指的是当前对象的其他方法
  17. * 3、调用成员变量的时候如何使用:
  18. * 当方法中的参数名称跟成员变量保持一致的时候,使用 this.变量名称 表示的是对象的值,而使用变量名称表示形参列表中的值
  19. */
  20. public class ThisDemo {
  21. //成员变量
  22. String name;
  23. int age;
  24. //构造器、构造方法
  25. public ThisDemo(){
  26. }
  27. public ThisDemo(String name){
  28. this.name = name;
  29. }
  30. public ThisDemo(String name,int age){
  31. // this.name = name;
  32. this(name);
  33. this.age = age;
  34. }
  35. public void test1(){
  36. System.out.println("test1被执行");
  37. // test2(); //也可以省略this来调用其他方法
  38. }
  39. public void test2(String name){
  40. System.out.println("test2被执行");
  41. this.test1();
  42. System.out.println(name);
  43. System.out.println(this.name);
  44. }
  45. public static void main(String[] args) {
  46. ThisDemo td = new ThisDemo("张飞",20);
  47. // System.out.println(td.name);
  48. // System.out.println(td.age);
  49. // td.test1();
  50. td.test2("赵云");
  51. }
  52. }

二、static关键字


1.在类中,用static声明的成员变量为静态成员变量 ,或者叫做: 类属性,类变量.


·它为该类的公用变量,属于类,被该类的所有实例共享,在类被载入时被显式初始化
·对于该类的所有对象来说,static成员变量只有一份。被该类的所有对象共享
·可以使用”对象.类属性”来调用。不过,一般都是用“类名.类属性”
·static变量置于方法区中


2.用static声明的方法为静态方法


·不需要对象,就可以调用(类名.方法名)
·在调用该方法时,不会将对象的引用传递给它,所以在static方法中不可访问非static的成员
·静态方法不能以任何方式引用this和super关键字


3.static示例代码

  1. public class TestStatic {
  2. int a;
  3. static int width;
  4. static void gg(){
  5. System.out.println("gg");
  6. }
  7. void tt(){
  8. System.out.println("tt");
  9. }
  10. public static void main(String[] args){
  11. TestStatic hi = new TestStatic();
  12. TestStatic.width = 2;
  13. TestStatic.gg(); //gg();
  14. //通过引用也可以访问static变量或static方法。不过,一般还是使用类名.static成员名来访问。
  15. hi.gg();
  16. gg();
  17. }
  18. }
  1. static关键字
  • 使用static声明的成员变量称为静态变量,
  • 使用static声明的方法称为静态方法
  • 静态变量和静态方法又称为类变量类方法
  • static关键字用法-课堂demo【重点掌握】
  1. package netclass02;
  2. /**
  3. * @Auther: Yu Panpan
  4. * @Date: 2022/1/7 - 01 - 07 - 15:17
  5. * @Description: netclass02
  6. * @version: 1.0
  7. */
  8. /*
  9. * 使用static统计在类中一共产生多少个对象?
  10. */
  11. public class StaticDemo2 {
  12. static int count;
  13. // int count;
  14. public StaticDemo2(){
  15. count++;
  16. System.out.println("创建了" +count + "个对象");
  17. }
  18. public static void main(String[] args) {
  19. new StaticDemo2();//1
  20. new StaticDemo2();//2
  21. new StaticDemo2();//3
  22. }
  23. }

 三、代码块
1.概念:使用”{}”括起来的一段代码
2.分类:根据位置可分类
·普通代码块—>直接在方法或语句中定义的代码块
·构造代码块—>直接写在类中的代码块
·静态代码块—>使用static声明的代码块
·同步代码块—>多线程的时候会学到
3.静态初始化块
·如果希望加载后,对整个类进行某些初始化操作,可以使用static初始化块。
·类第一次被载入时先执行static代码块;类多次载入时,static代码块只执行一次;static经常用来
·进行static变量的初始化。
·是在类初始化时执行,不是在创建对象时执行。
·静态初始化块中不能访问非static成员。
·demo
 

  1. public class TestStaticBlock {
  2. static {
  3. System.out.println("此处,可执行类的初始化工作!");
  4. }
  5. public static void main(String[] args) {
  6. System.out.println("main方法中的第一句");
  7. }
  8. }

4. 课堂demo【重点掌握】

  1. package netclass02;
  2. /**
  3. * @Auther: Yu Panpan
  4. * @Date: 2022/1/7 - 01 - 07 - 15:34
  5. * @Description: netclass02
  6. * @version: 1.0
  7. */
  8. /*
  9. * 代码块: 使用{}括起来的一段代码叫做代码块
  10. * 分类:
  11. * 普通代码块: 定义在方法中,使用{}括起来的代码叫做普通代码块
  12. * 构造代码块:定义在类中的使用{}括起来的代码叫做构造代码块
  13. * 注意:每次代码运行的时候会将构造代码块中的代码添加到构造方法的前面
  14. * 构造代码块中的代码会添加到每一个构造方法中,当使用this(参数)的时候不会添加
  15. * 静态代码块:使用static{}括起来的代码叫做静态代码块,在程序载入的时候优先执行
  16. * 数据库连接等其他提前需要准备好的代码会放在static代码块
  17. * 同步代码块:
  18. * 在使用多线程的时候会使用,用来给共享空间进行加锁操作(后面讲)
  19. * 执行顺序:静态代码块--》构造代码块(创建对象的时候才会用到)--》普通代码块
  20. */
  21. public class CodeBlockDemo {
  22. int x;
  23. int y;
  24. {
  25. System.out.println("构造代码块");
  26. }
  27. static{
  28. System.out.println("static代码块");
  29. // x = 100;
  30. }
  31. public CodeBlockDemo(){
  32. }
  33. public CodeBlockDemo(int x){
  34. System.out.println("CodeBlockDemo(int x)被执行了");
  35. this.x = x;
  36. }
  37. public CodeBlockDemo(int x,int y){
  38. this(x);
  39. System.out.println("CodeBlockDemo(int x,int y)被执行了");
  40. // this.x = x;
  41. this.y = y;
  42. }
  43. public void test1(){
  44. System.out.println("test1方法被执行");
  45. {
  46. System.out.println("test1方法中的普通代码块");
  47. }
  48. }
  49. public synchronized void test2(){
  50. //定义一个同步代码块(先见个面,讲到多线程的时候再详细讲。)
  51. // synchronized(this){
  52. //
  53. // }
  54. }
  55. public static void main(String[] args) {
  56. CodeBlockDemo cbd = new CodeBlockDemo();
  57. cbd.test1();
  58. {
  59. System.out.println("main方法中的普通代码块");
  60. }
  61. CodeBlockDemo cbd2 = new CodeBlockDemo(1,2);
  62. }
  63. }

四、package
1.为什么需要package?
·为了解决类之间的重名问题。
·为了便于管理类:合适的类位于合适的包
2.package怎么用?
·通常是类的第一句非注释性语句。
·包名:域名倒着写即可,再加上模块名,并不内部管理类。
3.注意事项:
·写项目时都要加包,不要使用默认包。
·com.tensent和com.tensent.oa,这两个包没有包含关系,是两个完全独立的包。只逻辑上看起来
·后者是前者的一部分。
4.JDK中的主要包
·java.lang
·包含一些Java语言的核心类,如String、Math、Integer、System和Thread,提供常用功能。
·java.awt
·包含了构成抽象窗口工具集(abstract window toolkits)的多个类,这些类被用来构建和管理应用·程序的图形用户界面(GUI)。
·java.net
·包含执行不网络相关的操作的类。
·java.io
·包含能提供多种输入/输出功能的类。
·java.util
·包含一些实用工具类,如定义系统特性、使用不日期日历相关的函数。
5. 课堂demo【重点掌握】
 

  1. package netclass02;
  2. import java.util.Date;
  3. import java.util.Scanner;
  4. /**
  5. * @Auther: Yu Panpan
  6. * @Date: 2022/1/7 - 01 - 07 - 16:24
  7. * @Description: netclass02
  8. * @version: 1.0
  9. */
  10. /*
  11. * package:包,对应到文件系统就是多级目录
  12. * 为了解决两个问题:
  13. * 1、文件同名问题
  14. * 2、为了方便管理类,将具体处理功能的代码放到同一个目录下
  15. * 使用:
  16. * 一般定义package会放置在java文件的第一行
  17. * package 域名的倒写
  18. * 比如:www.taobao.com
  19. * 包名:com.taobao.
  20. * package com.tensent.项目名称
  21. * 包名的命名规则:
  22. * 企业性质.企业名称.项目名称.模块名称. 子模块名称....
  23. * 企业性质:com org edu gov
  24. * 企业名称:alibaba baidu huawei tensent
  25. * 项目名称:oa erp hrms crm
  26. * 模块名称:ums--userManagementSystem 用户管理系统

联系站长

QQ:769220720

Copyright © SibooSoft All right reserved 津ICP备19011444号