OO先导课——JAVA初见懵的知识合集

常见操作

1.数组

//声明 *数组也是对象,长度是一个属性
int[] i=new int[5];
int[] i={0,1};

String[] str=new String[5];//字符串数组,双引号,与char字符数组区分
String[] str={"a","b"};
String name;//字符串能直接使用
char[] helloArray = { 'r', 'u', 'n', 'o', 'o', 'b'};
String helloString = new String(helloArray); //字符数组构造字符串


//求长度 数组在起始的时候,长度就 已经确定了(只是一个属性,‘’没用)
//参考:http://www.cnblogs.com/entry-android/p/5539362.html

n=charSet.length;

  字符串数组

    长度不是以结束符结束;

2.主类(有main()的类)

  (1)使用其他类实例的方法

  (2)使用自己的函数

import java.util.Vector;
public class GeometryManager { public static void main(String[] args) { countVolume(boxList, cylinderList,sphereList); } public static void countVolume(Vector<Box> blist, Vector<Cylinder> clist, Vector<Sphere> slist){ }
}

  (3)主类有属性必须是static的

    (马大姐的问题)在类中赋值语句不能出现在方法之外,除非声明一个变量时给他赋初值

 3.注意事项

  (1)JAVA中int不能当布尔用,flag要声明Boolean,赋值ture,false;(注意拼写)

4.基本概念

  (1)Java中的属性,通常可以理解为get和set方法(你想给他赋值就用u.setXXX();取这个类的对象的某个值 就get)。 

                           http://www.cnblogs.com/deogao/p/5320542.html

   字段,通常叫做“类成员”     

   http://blog.csdn.net/ljz2009y/article/details/8023499

  (2)static静态的、全局的

    1. 静态方法 :无需本类的对象即可调用此方法

class Simple {
    static void go() {
       System.out.println("Welcome");
    }
}
public class Cal {
    public static void main(String[] args) {
       Simple.go();
    }
}

     2.静态变量:声明为static的变量实质上就是全局变量

     http://www.cnblogs.com/lzq198754/p/5767066.html

继承


基本操作

//继承,只有构造方法不能被继承
public class  ScaleBox extends Box{
    public double scale;
    public ScaleBox(double w,double h,double d,double f){
        super(w*f,h*f,d*f);//直接使用box的构造方法,必须要第一行,先构造完才能用
        scale=f;
}

方法重载 

  相同的名字,但具有不同的参数和不同的定义

方法重写(覆盖)

  名字一样,返回值和参数一样,在子类中写了的话就成子类的新东西了,不能使用父类的原方法

 新知识  

1.ArrayList

  动态数组

  ArrayList 是一个数组队列,相当于 动态数组。与Java中的数组相比,它的容量能动态增长。它继承于AbstractList。

  http://www.cnblogs.com/skywang12345/p/3308556.html

2.容器——管理同类的对象(美滋滋)

   List

  http://blog.csdn.net/vaniice/article/details/6102015

  http://www.cnblogs.com/epeter/p/5648026.html

  (int)list.size()

3.Class<T> type泛型

  参数化类型

  http://www.cnblogs.com/lwbqqyumidi/p/3837629.html

4. vector

  Vector是矢量队列,它继承了AbstractList;

  

import java.util.Vector;

public class GeometryManager {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //initialize geometries.
        Vector<Box> boxList;
        boxList = new Vector<Box>();
        boxList.add(new Box(10,10,10,1));
        boxList.add(new Box(10,20,10,10));
        boxList.add(new Box(4,2,1,10));
        
        countVolume(boxList, cylinderList,sphereList);
        //求所有长方体的总体积    
    }
    
    public static void countVolume(Vector<Box> blist, Vector<Cylinder> clist, Vector<Sphere> slist){
        double ans=0;
        for(int i=0;i<blist.size();i++){
            ans+=blist.get(i).getVolume();
        }
        System.out.println("总体积是:"+ans);
    }

}

  http://blog.csdn.net/qq924862077/article/details/48039567

原文地址:https://www.cnblogs.com/iwanna/p/7127938.html