自己做网站最新视频教程广告联盟app推广
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里面引用funpubilc 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";}
}