方法重载Overload

来源:互联网 发布:java https cookie 编辑:程序博客网 时间:2024/05/16 09:52

方法重载:Overload

所谓方法重载:方法名字相同,方法参数的个数,类型,次序不同。

关于方法重载要注意三点:

1.参数次序不同,也构成方法重载

2.方法返回类型不同,不构成方法重载

3.构造方法重载,是方法重载最常见的形式

静态属性和方法

静态的属于类而不属于对象,即可以使用对象来访问也可以使用类名来访问,但是更推荐使用类名来访问

public class Person {String name;int age;//静态属性static int num = 0;Person(){System.out.println("一个人诞生了。。。");}Person(String name, int age){this();this.name = name;this.age = age;}//静态方法public static void worldPeace(){System.out.println("world peace");}public void thinking(){System.out.println(this.name+"正在思考ing。。。");num++;}public static void main(String[] args) {Person p1 = new Person("zhangsan", 18);p1.thinking();System.out.println(Person.num);System.out.println(p1.num);Person p2 = new Person("wangwu", 18);p2.thinking();System.out.println(Person.num);System.out.println(p2.num);Person.worldPeace();p1.worldPeace();p2.worldPeace();}}
输出结果

一个人诞生了。。。zhangsan正在思考ing。。。11一个人诞生了。。。wangwu正在思考ing。。。22world peaceworld peaceworld peace

静态代码块

//静态代码块static{System.out.println("静态代码块执行了。。。");}
静态代码块在构造方法之前执行,并且只执行一次。

栈内存和堆内存

栈内存:顺序结构

堆内存:离散结构

基本数据类型都是在栈内存中分配,引用数据类型都是在堆内存中分配。



0 0
原创粉丝点击