java编程思想2

public static void main(String[] args) {
    int[] a= {1,2,3};
    int[] b;
    b=a;
    for (int i = 0; i < b.length; i++) {
        b[i]+=10;
    }
    for (int i : a) {
        System.out.println(i);
    }
}

所以说数组是引用类型 而且a可以点出来Object的方法
static void printArray(Object ... args) {
    for (Object object : args) {
        System.out.print(object);
        System.out.print("---");
    }
    System.out.println("---------------");
}

public static void main(String[] args) {
    Object[] a=new Object[] {
            new Integer(47),
            new Float(3.14),
            new Double(11.11)
    };
    printArray(a);
    printArray(new Object[] {"aa","bb","cc"});
    printArray((Object[])new A[] {new A(),new A(),new A(),new A()});
    printArray((Object[])new Integer[] {new Integer(10)});
}
//可变参数列表就是数组 (Object[])new Integer[] {new Integer(10)转成Object[] 警告消失

static void getNum(Float...objects) {
    for (Float object : objects) {
        System.out.println(object);
    }
}


public static void main(String[] args) {
    getNum();
}
编译通过
static void getNum(Integer...objects) {
    for (Integer object : objects) {
        System.out.println(object);
    }
}
static void getNum(Float...objects) {
    for (Float object : objects) {
        System.out.println(object);
    }
}
public static void main(String[] args) {
    //getNum();//编译不通过 编译器不知道用哪个方法了
    
}
    
    
java解释器运行步骤:
    1:找出环境变量CLASSPATH
    2:寻找。class文件的根路径
    3:获取包名称把.替换成/
    4:没太懂 好像是CLASSPATH追加生成了一个根路径 产生关联 
    
    
静态导入
import static 可以直接定位在方法级别
package com.gouying.secpt;

public class Print {
    public static void print(Object object) {
        System.out.println(object);
    }
}
···
import static com.gouying.secpt.Print.print;
public class MyClass {
    public static void main(String[] args) {
        print("sss");
    }
}

package 包访问权限 默认的话对包内权限是public 对包外的权限是private
操作系统的CLASSPATH的.的意思是从当前文件作为起点 作用是同级的包中可以互相访问

继承的话也是如此

protected主要作用是为了继承问题可以访问,包含包访问权限

封装的概念:
    访问权限的控制被称为隐藏具体的实现
    数据和方法包装进类中以及具体实现的隐藏 成为封装
    
    结果是一个同时带有特征和行为的数据类型
    
toString 用法:当表达式 "source="+source 这个引用自动会调用toString 方法 如果没有 则返回地址

7.4.1如何正确清理没太看懂

继承可以重载基类方法,如果不想重载基类方法可以用@Override注解
is-a 使用继承
has-a 使用组合

final关键字的目的是:设计或者是效率
1:final修饰常量是固定不变的(但不是说在编译之前我们就知道这个值)
private static Random rand=new Random();
public String toString() {
    return id+": "+"i4="+i4+",INT_5="+INT_5;
}
private final int i4 =rand.nextInt(20);
static final int INT_5=rand.nextInt(20);

//或者在初始化时候赋值
System.out.println("打印出来的i和INT_5是随机的");
2:final修饰引用类型是地址不变但是值可以变  用static final可以修饰一个不能改变的存储空间

class Value{
    int i;
    public Value(int i) {
        this.i=i;
    }
}
public class FinalStudy {

private static Random rand=new Random();
private String id;
public FinalStudy(String id) {
    this.id=id;
}
private final int valueOne=9;
private static final int VALUE_ONE=99;
public static final int VALUE_THREE=39;
private final int i4 =rand.nextInt(20);
static final int INT_5=rand.nextInt(20);
private Value v1=new Value(11);
private final Value v2=new Value(22);
private static final Value VAL_3=new Value(33);
private final int[] a= {1,2,3,4};
public String toString() {
    return id+": "+"i4="+i4+",INT_5="+INT_5;
}
public static void main(String[] args) {
    FinalStudy fd1=new FinalStudy("fd1");
    //fd1.valueOne++; //The final field FinalStudy.valueOne cannot be assigned
    fd1.v2.i++;
    System.out.println(fd1.v2.i);
    fd1.v1=new Value(9);
    for (int i = 0; i < fd1.a.length; i++) {
        fd1.a[i]++;
        System.out.println(fd1.a[i]);
    }
    //fd1.v2=new Value(1);//The final field FinalStudy.v2 cannot be assigned
    //fd1.VAL_3=new Value(11);
    //fd1.a=new int[4];
    System.out.println(fd1);
    //fd1.i4=11;
    
}

初始化及类的加载: 每个类的编译代码都会存放在自己的独立文件中。创建对象时候开始加载 当访问static方法或者static域时候也会发生加载
初始使用之处也是static初始化发生之处 static只加载一次

不要因为效率的目的而使用final 而是因为设计

方法绑定:
    前期绑定:static final(final包括private)
    后期绑定:在运行时候我们才知道类型
如果是静态方法 不能多态

垃圾清理的顺序:
    1:子类顺序调用父类的dispose方法(向上初始化)
    2:按照成员变量的顺序相反清理(顺序初始化)

class Characteristic{
    private String s;
    public Characteristic(String s) {
        this.s=s;
        System.out.println("creating charactersitic"+s);
    }
    protected void dispose() {
        System.out.println("dispose characteristic"+s);
    }
}
class Description{
    private String s;
    public Description(String s) {
        this.s=s;
        System.out.println("creating descriptoon:"+s);
    }
    protected void dispose() {
        System.out.println("dispose Description:"+s);
    }
}
class LivingCreature{
    private Characteristic p=new Characteristic("is alive");
    private Description t=new Description("basic living creature");
    public LivingCreature() {
        System.out.println("livingcreture");
    }
    
    
    protected void dispose() {
        System.out.println("Livingcreature dispose");
        t.dispose();
        p.dispose();
    }
}
class Animal extends LivingCreature{
    private Characteristic p=new Characteristic("has heart");
    private Description t=new Description("Animal not Vegetable");
    public Animal() {
        System.out.println("Animal()");
    }
    protected void dispose() {
        System.out.println("Animal dispose");
        t.dispose();
        p.dispose();
        super.dispose();
    }
}
class Amphibian extends Animal{
    private Characteristic p=new Characteristic("can live in water");
    private Description t =new Description("Both water and land");
    public Amphibian() {
        System.out.println("Amphibian()");
    }
    protected void dispose() {
        System.out.println("Amphibian()");
        t.dispose();
        p.dispose();
        super.dispose();
    }
}
class Frog extends Amphibian{
    private Characteristic p=new Characteristic("Croaks");
    private Description t=new Description("eats bugs");
    public Frog() {
        System.out.println("frog()");
    }
    protected void dispose() {
        System.out.println("frog dispose");
        t.dispose();
        p.dispose();
        super.dispose();
    }
}
public class ExtendsAndDispose {
    public static void main(String[] args) {
        Frog frog=new Frog();
        System.out.println("bye");
        frog.dispose();
    }
}

计数清理---没太懂
class Shared{//共享
    private int refcount=0;
    private static long counter=0;
    private final long id=counter++;
    public Shared() {
        System.out.println("creating"+this);
    }
    public void addRef() {
        refcount++;
    }
    protected void dispose(){
        if(--refcount==0) {
            System.out.println("disposing"+this);
        }
    }
    public String toString() {
        return "shared"+id;
    }
}
class Composing{//作曲
    private Shared shared;
    private static long counter=0;
    private final long id = counter++;
    public Composing(Shared shared) {
        System.out.println("creating"+this);
        this.shared=shared;
        this.shared.addRef();
    }
    protected void dispose() {
        System.out.println("disposing"+this);
        shared.dispose();
    }
    public String toString() {
        return "Composing"+id;
    }
}
public class ReferenceCounting {
    public static void main(String[] args) {
        Shared shared=new Shared();
        Composing[] composing= {
                new Composing(shared),
                new Composing(shared),
                new Composing(shared),
                new Composing(shared),
                new Composing(shared)
        };
        for (Composing c : composing) {
            c.dispose();
        }
    }
}

初始化顺序:
    1:在调用GLYPH时候子类的对象radius初始化还是0
以下方法的忠告:构造器尽量避免调用其他方法,如果想要调用的话 调用final(包含private)
class Glyph{
    void draw() {
        System.out.println("Glyph.draw()");
    }
    Glyph(){
        System.out.println("before() Glyph.draw()");
        draw();
        System.out.println("after() Glyph.draw()");
    }
}
class RoundGlyph extends Glyph{
    private int radius=1;
    RoundGlyph(int r){
        radius=r;
        System.out.println("RoundGlyph.RoundGlyph()");
    }
    @Override
    void draw() {
        System.out.println("RoundGlyph.draw(),ranius="+radius);;
    }
}
public class PolyConstructors {
    public static void main(String[] args) {
        new RoundGlyph(5);
    }
}
 

转载于:https://my.oschina.net/u/4058227/blog/3020442

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值