review

2020-05-21  本文已影响0人  杜艳_66c4

内存分析结果

package review;

/**
 * Created by apple on 20/5/5.
 */
public class Gouzao {
    int id ;
    int age =20;

    Gouzao(int _id, int _age){
        id = _id;
        age =  _age;
    }

    public static void main(String[] args) {
        Gouzao tom = new Gouzao(1,25);   //调了构造方法,
         //构造方法把1,25 传递给了id,和age,id和age又传给本身对象的值
        System.out.println(tom.age);
        System.out.println(tom.id);
    }
}

输出:
25
1

较复杂的内存分析

package review;

/**
 * Created by apple on 20/5/5.
 */
public class Goouzao2 {
    public static void main(String[] args) {
        Goouzao2 test = new Goouzao2();

        int date = 9;
        BirthDate d1 = new BirthDate(7,7,2020);
        BirthDate d2 = new BirthDate(6,7,2020);
        test.change1(date);
        test.change2(d1);
        test.change3(d2);
        System.out.println("date" + date);
        d1.display();
        d2.display();
    }

    public void change1 (int i){
        i = 1234;
    }

    public void change2(BirthDate b){
        b = new BirthDate(11,17,2020);
    }

    public void change3(BirthDate b){
        b.setDay(30);
    }
}

class BirthDate {
    private int day;
    private int month;
    private int year;

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

//构造方法接受3个参数,通过三个参数,指定年月日
    public BirthDate( int d,int m, int y){
        day = d;
        month = m;
        year = y;
    }
    //打印
    public void display(){
        System.out.println("change:" + day + " " + month + " " + year);
    }
}

输出:
date9
change:7 7 2020
change:30 7 2020

更近一步的例子

import com.sun.corba.se.impl.oa.poa.POAPolicyMediatorImpl_NR_UDS;

/**
 * Created by apple on 20/4/11.
 */
class Point {
    //定义一个点类来表示三维空间中的点,有三个坐标,要求如下
    //可以生成具有特定坐标的点对象
    // 提供可以设置三个坐标的方法
    //提供可以计算该点到某点距离平方的方法

    double x;
    double y;
    double z;

    Point(double _x, double _y, double _z){
        x = _x;
        y = _y;
        z = _z;
    }
    void  setX(double _x){
        x = _x;
    }
    void  setY(double _y){
        y = _y;
    }
    void  setZ(double _z){
        z = _z;
    }

    double getDistance(Point p){
        return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y) + (z - p.z) * (z - p.z);
    }

}

public class TestPoint {
    public static void main(String[] args) {
        Point p = new Point(1.0 , 2.0, 3.0);
        Point p1 = new Point(0.0, 0.0, 0.0);
        System.out.println(p.getDistance(p1));

        p.setX(5.0);
        System.out.println(p.getDistance(new Point(1.0, 1.0, 1.0)));

    }
}

重载

package review;

/**
 * Created by apple on 20/5/5.
 */
public class OverLoad2 {
    public static void main(String[] args) {
        OverLoad2 t = new OverLoad2();
        t.max(0.1,0.6);
        t.max(3,6);


    }

    void max(int a,int b){
        System.out.println(a > b ? a:b);
    }
    void max(double a ,double b){
        System.out.println("浮点型");
        System.out.println(a > b ? a:b);
    }
}

toString

/**
 * Created by apple on 20/5/5.
 */
public class ToString {
    public static void main(String[] args) {
        DogToString dt = new DogToString();
        System.out.println("d" + dt.toString());
    }
}
class DogToString{}

输出:
dDogToString@6f94fa3e

/**
 * Created by apple on 20/5/5.
 */
public class ToString {
    public static void main(String[] args) {
        DogToString dt = new DogToString();
        System.out.println("d:" + dt.toString());
    }
}
class DogToString{
    public String toString(){
        return "I am a cruel dog";
    }
}

输出:d:I am a cruel dog

异常

package exception;
import java.applet.AppletContext;
import java.io.*;

/**
 * Created by apple on 20/5/2.
 */
public class demo1 {
    public static void main(String[] args) {
       try {
           int[] arr = {1,2,3};
           System.out.println(arr[0]);
       }catch (ArrayIndexOutOfBoundsException e){
           e.printStackTrace();
           System.out.println("出错了");
           e.getMessage();
       }

        FileInputStream in = null;
        try{
            in  = new FileInputStream(demo1.class.getResource("/").getPath()+"exception/myfile.txt");
            int b;
            b = in.read();
            System.out.println("aaa"+b);
            while(b != -1){
                System.out.println(b);
                 b = in.read();
            }
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            System.out.println(e.getMessage());
        }finally{
            try{
                in.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}
/*


*/

上一篇下一篇

猜你喜欢

热点阅读