J2SE基础:2.对象的创建与使用
来源:互联网 发布:mac右键怎么按 编辑:程序博客网 时间:2024/06/06 18:19
1:参数传递的值传递与引用传递
2:类变量与成员变量(实例变量,对象变量)
3:类方法与成员方法(实例方法,对象方法)
4:
构造方法
销毁方法(finalize)
5:static静态块与对象块
A:值传递:基本数据类型传递都是值传递
B:引用传递(地址传递):对象数据类型都是引用传递。
2:类变量与成员变量(实例变量,对象变量)
类变量:通过类名调用,类变量被所有的实例共享。
final static int MAX = 20;//Java中定义常量
对象变量:通过对象调用(对象必须new出来)。
3:类方法与成员方法(实例方法,对象方法)
类方法:通过类名调用,在类方法中不能使用this关键字。
因为this代表当前对象。
成员方法:通过对象调用(对象必须new出来)。
4:
构造方法
销毁方法(finalize)
销毁方法是在对象被销毁的时候进行调用的。
当一个对象在堆区没有一个明确的引用指向它的时候,Java虚
拟机认为该对象是无用的。
垃圾回收器是用于回收堆区分配的对象。 垃圾回收器只会回收3次的
的内存。垃圾回收器是虚拟机自动调用的。(堆区内存不够的情况下调用)
但是可以通过System.gc()来强制运行垃圾回收器。
5:static静态块与对象块
寻找main方法--->加载类--->加载类的静态块代码(只初始化一次)
--->加载类的静态方法和静态变量(只初始化一次)---->对象块方法--->对象的构造方法--->调用对象的方法--->执行对象的销毁方法。
//成绩类class Score{int english;int math;int chinese;Score(){}Score(int english,int math,int chinese){this.english = english;this.math = math;this.chinese = chinese;}}class Student{int stuid;String stuname;String stusex;//将成绩类做为学生类的一个属性。Score score;public Student(Score score){this.score = score;}public int getTotalScore(){return this.score.english +this.score.math +this.score.chinese;}public void changeScore(Score score){score.chinese = 0;score.math = 0;}}public class Test_02{public static void main(String args[]){Score score_one = new Score(70,60,65);//score_one.english = 70;//score_one.math = 60;//score_one.chinese = 65;Score score_two = new Score();score_two.english = 11;score_two.math = 12;score_two.chinese = 13;Score score_three = new Score(45,46,47);//score_three.english = 45;//score_three.math = 46;//score_three.chinese = 47;Student stu_one = new Student(score_three);Student stu_two = new Student(score_two);Student stu_three = new Student(score_one);/*System.out.println(stu_one.getTotalScore());System.out.println(stu_two.getTotalScore());score_three.english = 70;stu_one.score.math = 23;System.out.println(stu_one.getTotalScore());System.out.println(stu_two.getTotalScore());*/System.out.println(stu_one.getTotalScore());//138stu_one.changeScore(score_two);System.out.println(stu_one.getTotalScore());//138stu_one.changeScore(score_three);System.out.println(stu_one.getTotalScore());//45System.out.println(stu_two.getTotalScore());}}
public class Test_03{int id;final static int MAX = 20;public static void main(String args[]){//Test_03 test = new Test_03();//System.out.println(test.MAX);System.out.println(Test_03.MAX);}}
class Person{int personid;String personname;public Person(){System.out.println("对象的构造方法");this.personid = 1;this.personname = "中国人";}public void method(){System.out.println("执行方法");}public void finalize(){System.out.println("对象被销毁了");this.personid = 0;this.personname = null;}}public class Test_04{public static void main(String args[]){Two();System.gc();}public static void Two(){//创建对象Person person = new Person();//用对象person.method();}}
public class Test_05{//加载类时,最早执行的一块初始化内容。static{System.out.println("静态块");}//加载类时,静态方法与静态变量都已经放到内存的静态区域中了。public static void staticMethod(){System.out.println("static方法");}//对象块的内容,在对象初始化之前执行的内容{System.out.println("对象块方法");}//对象的构造方法public Test_05(){System.out.println("构造方法");}public void objectMethod(){System.out.println("对象方法");}public static void main(String args[]){Test_05.staticMethod();Test_05 test = null;test = new Test_05();test.objectMethod();Test_05 test2 = null;test2 = new Test_05();test2.objectMethod();}}class Two{static{System.out.println("Two的静态块");}}
0 0
- J2SE基础:2.对象的创建与使用
- J2SE基础:3.对象的创建与使用类的命名空间和访问权限
- j2se之对象的创建过程
- js基础进阶2-2 面向对象(类与对象的创建与使用)
- 对象的创建与使用
- J2SE基础夯实系列之List中的数据如何根据对象的某一个或多个字段排序引出Comparable和comparator的使用-- Collections与Arrays
- J2SE基础之Random类的使用
- J2SE基础之Scanner的使用
- J2SE之面向对象基础
- J2SE基础:4.面向对象的特性一
- J2SE基础:5.面向对象的特性2
- J2SE基础:6.面向对象的特性3
- j2se 数组的创建
- 默认值对象的创建与使用
- 第二章,对象的创建与使用
- SAP权限对象的创建与使用
- 【JAVA】32、对象的创建与使用
- 4.2 C++对象的创建与使用
- jquery实现鼠标悬停单元格变色
- 远程jvm调试-备份
- 常见控件的使用方法(一)
- 关于easyui的layout的region的resize的题目(自适应浏览器)
- 大数据与未来社会
- J2SE基础:2.对象的创建与使用
- Eclipse常用开发插件
- Activity的生命周期(一)
- 硬盘如何分区及分区注意事项
- 《初出茅庐》
- Tomcat报内存溢出
- Activity的生命周期(二)
- 获取画到到view上的bitmap
- 生成二维码和扫描二维码demo分享