4月8号到13号学习记录(1)

这两天学习状态不好,学了这么些天也学的不是很多,还一直都没有做记录。。深刻检讨!!!

上次学到了面向对象、对象和类的概念,还有对象的创建以及对象的使用。知道了面向对象的三大特征:封装、继承和多态。然后这几天就主要学习了封装、继承和多态的相关概念及用法,还有构造方法、this关键字、static关键字、final关键字等。而且换了编程的方法,之前一直是用Dos窗口,现在用的是IDEA软件。

一、封装

在之前没有封装的时候,我们要想访问其他类中的实例变量时,只需要先创建对象然后就可以访问该变量并且可以修改他的值,显然这样时不够安全的,因此需要对其进行封装。

思想就是:首先,将以前声明的变量全部私有化,以前定义变量是:int i;,私有化就是:private int i;,变量私有化之后其他方法是无法直接通过“引用.变量名”的方法来访问或修改该变量的。

然后,要想让该变量被访问,但是主动权要落在变量的手中,或者说其他方法可不可以访问变量是由变量所在的类来决定的,我们当然就想到要在这个类里写一个函数,来控制访问的条件。这里有两个方法一个是get,一个是set,如下:

 

这样一来,其他方法想要访问这个变量是无法访问,但是方法没有被私有化,可以通过访问get/set方法来访问该变量。

还有一点需要注意的是,get/set 方法的修饰符列表为public,以往都是public static。其中,修饰符列表为public static的方法叫做静态方法,修饰符列表为public 的方法叫做实例方法。静态方法的访问格式为:类名.方法名,实例方法的访问格式为:引用.方法名。这里get/set 方法是实例方法,他们的访问格式为:引用.方法名。

最后,上面的步骤结束后,其实变量的安全性并未提升,所以我们还需要在set/get方法中加入条件语句。

以一个例子再说明继承:

 

 

 

二、构造方法

构造方法又叫构造函数、构造器、Constructor,其语法结构为:【修饰符列表】 构造方法名(形式参数列表){构造方法体;} ,与普通方法的语法结构不同的地方有三点,第一,构造方法的方法名必须要和类名相同;第二,构造方法不写返回值类型,也不写return语句,但是这并不代表构造方法没有返回值类型,他的返回值类型就是自己,而返回值是一个地址指向所在的类;第三,构造方法的调用方式是:new 构造方法名(实参列表)。

其实由上面的第三点就不难看出,构造方法就是用来创建对象的,同时还会初始化实例变量。

还有一个类里面可以不写任何构造方法,系统会默认提供一个无参数的构造方法,该构造方法叫缺省构造器,这也就是为什么之前的学习过程中,并没有在类里写过任何的构造方法,但是我们却可以调用构造方法来创建对象。当然构造方法有无参的也有实参的,一旦在类里写了一个实参构造器,那么系统将不再默认提供无参构造器,如要使用则需在类里自己定义。

以下是一段代码为例:

Test02类:

package com.Constructor.Test02;

public class Test02 {
    public static void main(String[] args) {
        new User(3);

        int j = sum(5,3);
        Test02 t = new Test02();
        t.dosome();
        System.out.println(j);
    }
    public static int sum(int a,int b){
        int c;
        c = a + b;
        return c;
    }
    public void dosome(){
        System.out.println("dosome已执行!");
    }
}

User类:

package com.Constructor.Test02;

public class User {
    public User(int i){
        int b = 6;
        i = i+b;
        System.out.println(i);

    }
}

三、参数传递

总结一句话就是传变量里存的东西,而且是复制,传的时候,原变量的值不会改变。

package com.elementpass;

public class Test01 {
    public static void main(String[] args) {
        //说明:此例为参数传递时传递的是字面值,即i里存放的是字面值3
        int i = 3;
        add(i);//此处将i的值复制并传进add方法,
        System.out.println("main--->"+i);//i的值传进方法后并未有返回值,故i里存放的依旧是3
    }
    public static void add(int i){
        i++;
        System.out.println("add--->"+i);//4
    }
}
package com.elementpass;

public class Test02 {
    public static void main(String[] args) {
        //该例说明:u里面存放的是对象U的地址,所以当调用方法change时,传过去的是U的地址,通过u访问并改变了U里存放的color1的值
        //因此在change方法结束后,输出的u.color1是改变后的结果,而与字面值的传递不同
        String color = "red";
        U u = new U(color);
        System.out.println(u.color1);//red
        change(u);
        System.out.println(u.color1);//black

    }
    public static void change(U u){
       u.color1 = "black";
        System.out.println(u.color1);//black
    }
}
class U{
    String color1 ;
    public U(String color){
        color1 = color;
    }

}

四、关键字this

this用在构造方法和实例方法当中,this是一个引用,是一个变量,保存着一个内存地址指向自身,this存在于对象内部。以代码为例:

package com.thisPractice;

public class Test01 {
    int i = 3;

    //该例说明:带有static关键字的方法中是不能使用this的,因为这种方法的调用不需要创建对象,如果要在其中使用就必须先创建对象
    public static void main(String[] args) {
        dosome();
        Test01 t = new Test01();
        t.doother();
        dosome();

    }
    public static void dosome(){
        System.out.println("dosome has done!");
        //System.out.println(this);
        Test01 tt = new Test01();
        System.out.println(tt.i);
    }
    public void doother(){
        System.out.println("doother has done!");
        System.out.println(this.i);
    }
}
package com.thisPractice;

public class Test02 {
    public static void method1(){
        //分别用完整形式和省略形式调用dosome和doother,并访问变量i
        System.out.println("method1 has done!");

        //完整形式
        Test02.dosome();
        Test02 tt = new Test02();
        tt.doother();
        System.out.println(tt.i);
        //省略形式
        dosome();
        //doother方法没有省略调用方法
    }
    public void method2(){
        System.out.println("method2 has done!");

        //完整形式
        Test02.dosome();
        Test02 ttt = new Test02();
        ttt.doother();
        System.out.println(ttt.i);
        //省略形式
        dosome();
        doother();//main函数在调用method2时,已经创建过了对象,这里可以不用创建对象直接this.doother就相当于t.doother,
                  //doother又是一个实例方法,this.可以省略
        System.out.println(i);
    }

    public static void main(String[] args) {
        //分别用完整的形式和省略形式调用method1和method2
        //完整形式
        Test02.method1();
        Test02 t = new Test02();
        t.method2();
        //省略形式
        method1();
        //method1没有省略调用方法

    }
    int i = 10;
    public static void dosome(){
        System.out.println("dosome has done!");
    }
    public void doother(){
        System.out.println("doother has done!");

    }
}
/*
//结果输出为:
  method1 has done!
  dosome has done!
  doother has done!
  10
  dosome has done!
  method2 has done!
  dosome has done!
  doother has done!
  10
  dosome has done!
  doother has done!
  10
  method1 has done!
  dosome has done!
  doother has done!
  10
  dosome has done!
*/
package com.thisPractice;

public class Test03 {
    public static void main(String[] args) {
        //在这里调用dosome方法
        Test03.dosome();
        //创建对象来调用
        Test03 t = new Test03();
        t.dosome();//此处可见,带有static 的方法也可以用"引用."的方式来访问
        //让t为空
        t = null;
        t.dosome();//t为空,但是依然可以访问dosome 方法,没有出现空指针异常,
        // 说明用"引用."的方法访问带有static 的方法其实质是系统将引用处理成了类名,并没有用到对象

    }
    public static void dosome(){
        System.out.println("dosome begin!");
    }
}

package com.thisPractice;

public class Test04 {
    int i = 0;
    public static void main(String[] args) {
        int i = 2;
        Test04 t = new Test04();
        t.sum(i);
        System.out.println(t.i);
    }
    public int sum(int i){
        this.i = i;//这里的this 不能省略,用于区分局部变量和成员变量;
        return i;
    }
}
package com.Constructor.Test03;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

public class Date {
    int year;
    int month;
    int day;



    public Date(){
        /*this.year = 2001;
        this.month = 9;
        this.day = 1;*///这样写太罗嗦,可以在该构造方法中调用另一个构造方法
        //但是需要注意的是,不可以在该方法中创建对象来调用另一个方法,因为在构造方法外部要先创建对象才能访问构造方法,这样就会有两个对象
        //应采用一下的方式调用
        this(2001,9,1);
        //注意,this(??,??,??);,这样的语句只能放在方法的第一行,也就是说,一个方法中值允许出现一次这样的代码

    }
    public Date(int year,int month,int day){
        this.year = year;
        this.month = month;
        this.day = day;
        System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
    }

}
package com.Constructor.Test03;

public class Test03 {
    public static void main(String[] args) {
        new Date();
        new Date(2022,4,12);
    }



}

我的电脑坚持不住了,晚上断电。就先写到这里,剩下还有,static/继承/多态/方法覆盖/final/package和Import,(好像剩的多了点哈哈哈,)明天再写吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值