大家好,欢迎来到IT知识分享网。
目录
1.构造方法
1.1 构造方法定义
构造方法是与类名同名,并且不设置返回值符号,在调用类时自动被调用的方法。可以理解为类的初始化方法,实际上定义类时会存在一个默认的构造方法,但不具有方法体,也就是实际上无任何效果。
1.2 构造方法语法
public class Demo01 { public Demo01(){ //没有返回值的标识符 //方法名和类名相同 System.out.println("你好");//构造方法的方法体 }
1.3 构造方法的作用
构造方法是在调用类创建对象时,自动被调用的方法,作用一般是对对象的属性进行初始化。
public class Demo01 { public Demo01(){ System.out.println("你好"); } public static void main(String[] args) { Demo01 demo01 = new Demo01(); //创建类的对象时会直接调用该类的构造方法 } }
2.方法的重载
2.1 方法重载的定义
2.2 普通方法的重载
2.3 构造方法的重载
public class Test { public Test(){ System.out.println("无参构造方法"); } public Test(int a){ System.out.println("单参构造方法 a:"+a); } public Test(int a, String b){ System.out.println("无参构造方法 a:"+a +"b:"+b); } public static void main(String[] args) { new Test(); new Test(1); new Test(1,"1"); } }
3.static
3.1 static成员的调用
public class Demo04 { //static static String name; public static void main(String[] args) { //可以不创建对象直接给static属性赋值 Demo04.name = "旺财"; //可以直接调用类的属性 System.out.println(Demo04.name);//结果为“旺财” //给属性赋值后,是永久改变的,当创建其他对象后,其属性也会变为该值 Demo04 demo04 = new Demo04(); System.out.println(demo04.name);//结果为“旺财” Demo04 demo041 = new Demo04(); //改变一个对象的属性,也会同时改变其他对象的属性 //意味着这个属性是所有对象公有的 demo041.name = "土豆"; System.out.println(demo041.name); System.out.println(demo04.name); System.out.println(Demo04.name); //三个结果都是“土豆” } }
3.2 static的内存空间
简单画一下,比较简陋,如果不正确,请指教。
3.3 static 和 无static 的区别
4.封装
4.1 什么是封装
4.2 封装的步骤
1.使用关键字private对类的属性进行隐藏(方法一般不用隐藏)
2.利用setter/getter方法对属性值进行操作
3.可以在方法中加入条件控制语句,进行限制
4.3 封装示例
public class Person { private int id; private String pwd; private String health; public Person(){ System.out.println("欢迎回来"); } public Person(int inputId){ this.id = inputId; System.out.println("欢迎回来"); } public Person(int inputId,String inputPwd){ this.id = inputId; this.pwd = inputPwd; System.out.println("欢迎回来"); } public Person(int inputId,String inputPwd,String inputHealth){ this.id = inputId; this.pwd = inputPwd; this.health = inputHealth; System.out.println("欢迎回来"); } public int getId() { return id; } public String getHealth() { return health; } public String getPwd() { return pwd; } public void setId(int id) { this.id = id; } public void setPwd(String pwd) { this.pwd = pwd; } public void setHealth(String health) { this.health = health; } }
public class PersonTest { public static void main(String[] args) { Person person = new Person(2,"","健康"); System.out.println(person.getId()); System.out.println(person.getPwd()); System.out.println(person.getHealth()); } }
4.4 this
4.4.1 构造方法中this的使用
对自身构造方法的调用,this()只能写在构造方法中,且需要放在第一行,而且要避免形成死循环。
public class TestThis { static int a; public TestThis(){ System.out.println("你好"); } public TestThis(int a){ this(); System.out.println("你好"+a); } public TestThis(String b){ this(a); System.out.println("你好"+b); } }
public class Test { public static void main(String[] args) { TestThis testThis = new TestThis("小明"); } }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/122056.html