Java类和对象——快速自动生成带参数的结构

1.鼠标点击右键或者(使用快捷按键:Alt+Insert)

2.选着generate

3.选择想要执行的指令

其中Constructor---构造方法(声明了private属性然后直接使用即可),生成带参数的结构

1:不带参数的结构(手动打上去)

2:带一个参数的结构(Constructor自动勾选打出)

3:带多个参数的结构(Constructor自动勾选打出)

                按住ctrl+鼠标左键点击,即可多选

声明了private属性只能在当前类里面使用

class Student{
private String name;

public Student(String name){
this.name;
}
}

//在Test1中使用就会报错
public class Test1 {
    public static void main(String[] args) {
        Student student=new Student();
        student.name="zhangsan";
        student.age=19;
        System.out.println(student.name);
    }
}

那么如果要使用的话如何操作呢?

使用   Getter and Setter,然后再主函数中声明。如果有打Getter了,那么可以直接选择Setter;另一个同理

public class Test1 {
    public static void main(String[] args) {
        Student student=new Student();
        student.setName("zhangsan");

        //使用快捷按键组合
        //student.getName().sout;

        //即可形成这个打印
        System.out.println(student.getName());
    }
}

静态方法和动态方法的区别

1.什么是静态方法(static)

public class Test1 {
    //拥有static修饰的就是静态方法
    public static void main(String[] args) {
        System.out.println(student.name);
    }
}

2.静态方法不能调用动态方法

public class Test1 {

    //eat和fun都是动态方法
    //可以在eat里面引用fun
    pubilc void eat{
     fun();
}

    pubilc void fun{
     System.out.println(this.name);
}

    //拥有static修饰的就是静态方法
    public static void main(String[] args) {
        //下面这两中方式不能使用
        System.out.println(student.name);
        System.out.println(this.name);
    }
}

3.加上static可以保持共同的变量

public class Test1 {

    //拥有static修饰的就是静态方法
    public static void main(String[] args) {
        //声明3个成员量,都在109班上课
        Test1 test1 = new Test1("A",18);
        Test1 test2 = new Test1("B",12);
        Test1 test3 = new Test1("C",10);
        
        //静态
        Test1.classRoom="109";
    }
}

代码块---静/动态代码块

特点

1.静态代码块最先执行

2.静态代码块只执行一次

3.静态代码块如果有多个的话,谁位置在前面就先执行谁

4.静态代码块如果被加载就会被执行

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值