1:參数传递的值传递与引用传递

A:值传递:基本数据类型传递都是值传递



B:引用传递(地址传递):对象数据类型都是引用传递。

2:类变量与成员变量(实例变量,对象变量)

类变量:通过类名调用,类变量被全部的实例共享。



final static int MAX = 20;//Java中定义常量







对象变量:通过对象调用(对象必须new出来)。

3:类方法与成员方法(实例方法,对象方法)

类方法:通过类名调用,在类方法中不能使用thiskeyword。

由于this代表当前对象。

成员方法:通过对象调用(对象必须new出来)。

4:

  构造方法

  销毁方法(finalize)

销毁方法是在对象被销毁的时候进行调用的。

当一个对象在堆区没有一个明白的引用指向它的时候,Java虚

拟机觉得该对象是没用的。

垃圾回收器是用于回收堆区分配的对象。

垃圾回收器仅仅会回收3次的

  的内存。

垃圾回收器是虚拟机自己主动调用的。(堆区内存不够的情况下调用)



  可是能够通过System.gc()来强制执行垃圾回收器。

5:static静态块与对象块

寻找main方法--->载入类--->载入类的静态块代码(仅仅初始化一次)



--->载入类的静态方法和静态变量(仅仅初始化一次)---->对象块方法

--->对象的构造方法--->调用对象的方法--->运行对象的销毁方法。

  1. //成绩类
  2. class Score
  3. {
  4. int english;
  5. int math;
  6. int chinese;
  7. Score(){
  8.  
  9. }
  10.  
  11. Score(int english,int math,int chinese){
  12. this.english = english;
  13. this.math = math;
  14. this.chinese = chinese;
  15. }
  16. }
  17.  
  18. class Student{
  19. int stuid;
  20. String stuname;
  21. String stusex;
  22. //将成绩类做为学生类的一个属性。
  23.  
  24. Score score;
  25.  
  26. public Student(Score score){
  27. this.score = score;
  28. }
  29.  
  30. public int getTotalScore(){
  31. return this.score.english +this.score.math +this.score.chinese;
  32. }
  33.  
  34. public void changeScore(Score score){
  35. score.chinese = 0;
  36. score.math = 0;
  37. }
  38. }
  39.  
  40. public class Test_02{
  41. public static void main(String args[]){
  42. Score score_one = new Score(70,60,65);
  43. //score_one.english = 70;
  44. //score_one.math = 60;
  45. //score_one.chinese = 65;
  46.  
  47. Score score_two = new Score();
  48. score_two.english = 11;
  49. score_two.math = 12;
  50. score_two.chinese = 13;
  51.  
  52. Score score_three = new Score(45,46,47);
  53. //score_three.english = 45;
  54. //score_three.math = 46;
  55. //score_three.chinese = 47;
  56.  
  57. Student stu_one = new Student(score_three);
  58. Student stu_two = new Student(score_two);
  59. Student stu_three = new Student(score_one);
  60.  
  61. /*
  62. System.out.println(stu_one.getTotalScore());
  63. System.out.println(stu_two.getTotalScore());
  64.  
  65. score_three.english = 70;
  66.  
  67. stu_one.score.math = 23;
  68.  
  69. System.out.println(stu_one.getTotalScore());
  70. System.out.println(stu_two.getTotalScore());
  71.  
  72. */
  73. System.out.println(stu_one.getTotalScore());//138
  74.  
  75. stu_one.changeScore(score_two);
  76.  
  77. System.out.println(stu_one.getTotalScore());//138
  78.  
  79. stu_one.changeScore(score_three);
  80.  
  81. System.out.println(stu_one.getTotalScore());//45
  82.  
  83. System.out.println(stu_two.getTotalScore());
  84. }
  85. }
  1. public class Test_03
  2. {
  3. int id;
  4. final static int MAX = 20;
  5.  
  6. public static void main(String args[]){
  7. //Test_03 test = new Test_03();
  8. //System.out.println(test.MAX);
  9. System.out.println(Test_03.MAX);
  10.  
  11. }
  12. }
  1. class Person
  2. {
  3. int personid;
  4. String personname;
  5.  
  6. public Person(){
  7. System.out.println("对象的构造方法");
  8. this.personid = 1;
  9. this.personname = "中国人";
  10. }
  11.  
  12. public void method(){
  13. System.out.println("运行方法");
  14. }
  15.  
  16. public void finalize(){
  17. System.out.println("对象被销毁了");
  18. this.personid = 0;
  19. this.personname = null;
  20. }
  21. }
  22. public class Test_04
  23. {
  24. public static void main(String args[]){
  25. Two();
  26.  
  27. System.gc();
  28. }
  29.  
  30. public static void Two(){
  31. //创建对象
  32. Person person = new Person();
  33.  
  34. //用对象
  35. person.method();
  36. }
  37.  
  38. }
  1. public class Test_05
  2. {
  3. //载入类时。最早运行的一块初始化内容。
  4.  
  5. static{
  6. System.out.println("静态块");
  7. }
  8.  
  9. //载入类时,静态方法与静态变量都已经放到内存的静态区域中了。
  10.  
  11. public static void staticMethod(){
  12. System.out.println("static方法");
  13. }
  14.  
  15. //对象块的内容,在对象初始化之前运行的内容
  16. {
  17. System.out.println("对象块方法");
  18. }
  19. //对象的构造方法
  20. public Test_05(){
  21. System.out.println("构造方法");
  22. }
  23.  
  24. public void objectMethod(){
  25. System.out.println("对象方法");
  26. }
  27.  
  28. public static void main(String args[]){
  29. Test_05.staticMethod();
  30.  
  31. Test_05 test = null;
  32. test = new Test_05();
  33. test.objectMethod();
  34.  
  35. Test_05 test2 = null;
  36. test2 = new Test_05();
  37. test2.objectMethod();
  38. }
  39. }
  40. class Two
  41. {
  42. static{
  43. System.out.println("Two的静态块");
  44. }
  45. }

J2SE基础:2.对象的创建与使用的更多相关文章

  1. [ Java学习基础 ] Java对象的创建和销毁

    类实例化可生成对象,实例方法就是对象方法,实例变量就是对象属性.一个对象的生命周期包括三个阶段:创建.使用和销毁. 创建对象 创建对象包括两个步骤:声明和实例化. 声明 声明对象与声明普通变量没有区别 ...

  2. [C++基础]关于对象的创建及内存分配

    测试: #include <stdio.h>#include <QDebug> class KPoint{public: KPoint(int x, int y){ nx = ...

  3. Java基础(2)面向对象和封装,对象的创建和使用、java对象的内存图

    1 类和对象 类:是一类事物的描述,抽象的.猫 对象:是一类事物的实例,具体的.某只猫 2 类的定义 成员变量和成员方法 //定义一个学生类 public class Student { //成员变量 ...

  4. J2SE基础题

    J2SE基础 八种基本数据类型的大小,以及他们的封装类.(有的也说是9中基本数据类型,包括了void) 基本类型 大小(字节) 默认值 封装类 byte 1 (byte)0 Byte short 2 ...

  5. JavaScript 基础回顾——对象

    JavaScript是基于对象的解释性语言,全部数据都是对象.在 JavaScript 中并没有 class 的概念,但是可以通过对象和类的模拟来实现面向对象编程. 1.对象 在JavaScript中 ...

  6. 图解JAVA对象的创建过程

    前面几篇博文分别介绍了JAVA的Class文件格式.JVM的类加载机制和JVM的内存模型,这里就索性把java对象的创建过程一并说完,这样java对象的整个创建过程就基本上说明白了(当然你要有基础才能 ...

  7. python基础——获取对象信息

    python基础——获取对象信息 当我们拿到一个对象的引用时,如何知道这个对象是什么类型.有哪些方法呢? 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type( ...

  8. SQL Server 2008空间数据应用系列四:基础空间对象与函数应用

    原文:SQL Server 2008空间数据应用系列四:基础空间对象与函数应用 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2008 R2调测. ...

  9. VBS基础篇 - 对象(1) - Class对象

    VBS基础篇 - 对象(1) - Class对象   相信对JAVA有一定了解的朋友一定对类这个名词不陌生,但是大家可能没有想过在VBS中使用Class类吧,其实Class类在自动化测试中是相当常用的 ...

随机推荐

  1. bzoj1193: [HNOI2006]马步距离(贪心+bfs)

    1193: [HNOI2006]马步距离 题目:传送门 题解: 毒瘤题... 模拟赛时的一道题,刚开始以为是一道大难题...一直在拼命找规律 结果.... 还是说正解吧: 暴力的解法肯定是直接bfs, ...

  2. 15.map映射

    #include <iostream> #include <map> #include <cstring> using namespace std; //map常规 ...

  3. BZOJ 3240 构造矩阵+矩阵快速幂

    思路: ax+b cx+d 构造矩阵+矩阵快速幂 (需要加各种特判,,,,我好像加少了- ) //By SiriusRen #include <cstdio> #include <c ...

  4. java9新特性-17-智能Java编译工具

    1.官方Feature 139: Enhance javac to Improve Build Speed. 199: Smart Java Compilation, Phase Two 2.使用说明 ...

  5. Pepper plugin implementation

    For Developers‎ > ‎Design Documents‎ > ‎ Pepper plugin implementation This document provides a ...

  6. QQ音乐

    import re import requestsimport json class Search: def __init__(self, song): ''' self.vkey_url ---&g ...

  7. cacti1.1安装报错

    安装过程中出现以下报错: ERROR: Your MySQL TimeZone database is not populated. Please populate this database bef ...

  8. hdu5336XYZ and Drops

    题意:给出r*c的网格,有的网格为空.有的有水.再给出一个爆炸点,从这个点向四周爆出四个水滴,若碰到水则融为一体,若碰到其它水滴直接跑过去互不影响.每秒可跑一格,若水中水滴数量超过4则爆开.问T秒后网 ...

  9. [Python] Accessing Array Elements

    NumPy Reference: Indexing Note: Indexing starts at 0 (zero). def slicing(): a = np.random.rand(5,4) ...

  10. [Python] Array Attributes of Numpy lib

    Attributes of numpy.ndarray: numpy.ndarray.shape: Dimensions (height, width, ...) numpy.ndarray.ndim ...