国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Java筆記(五 數(shù)組 對象的克隆 封裝類 Class類 Runtime類和Process類 設(shè)計模式)

Java筆記(五 數(shù)組 對象的克隆 封裝類 Class類 Runtime類和Process類 設(shè)計模式)

  數(shù)組:
int[] num=new int[3];//聲明num變量在棧內(nèi)存里,new是在堆內(nèi)存中給對象分配了空間
for(int i=0;i<num.length;i++)
{
         System.out.println(num[i]);
}
--------------------------------------------------------------
class Stringtest
{
      public static void main(String[] args)
      {
             Student[] students;
             students=new Student[3];
             students[0]=new Student("lisi",18);
             for(int i=0;i<students.length;i++)
             {
                      System.out.println(students[i]);
             }
class Student
{
      String name;
      int age;
      Student(String name,int age)
      {
             this.name=name;
             this.age=age;
      }
}
 
  數(shù)組的相關(guān)操作:
    在Java中,所有的數(shù)組都有一個缺省的屬性length,用于獲取數(shù)組中元素的個數(shù)
    數(shù)組的復(fù)制:System.arraycopy()
        public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)  Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
    數(shù)組的排序:Arrays.sort()
        在java.util包中。
    在已排序的數(shù)組中查找某個元素:Arrays.binarySearch(),返回值為int型,是要查找的數(shù)組的索引
        /*int[] num=new int[]{3,1,2};
        Arrays.sort(num);
        int index=Arrays.binarySearch(num,3);*/
        如果是對對象排序的話sort(Object[] a),All elements in the array must implement the Comparable interface,接口Comparable在java.lang包下
---------------------------------------------------------------------------
import java.util.Arrays;
class ArrayTest1
{
      public static void main(String[] args)
      {
           Student[] ss=new Student[]{new Student("zs",18),
                                      new Student("ls",19),
                                      new Student("ww",20)};
           Arrays.sort(ss);
           int index=Arrays.binarySearch(ss,new Student("ls",19));
           System.out.println("index="+index);
           if (index > -1)
       {
          System.out.println(ss[index]);
       }
           else
    System.out.println("Not Exists");
          
      }
}
     
class Student implements Comparable

        int num;
        String name;
        Student(String name,int num)
        {
              this.num=num;
              this.name=name;
        }
        public String toString()
        {
            return "num="+num+","+"name="+name;
        }
        public int compareTo(Object o)
        {
              Student s=(Student)o;
              //int retsult=num>s.num ? 1 : (num==s.num ? 0 : -1);這是第一種比較方法,下面定義了第二種比較方法
              int  result=num>s.num ? 1 : (num==s.num ? 0 : -1);
              if (result==0)
              {
                       result=name.compareTo(s.name);
              }
              return result;
        }
}
------------------------------------------------------------------------------------

  main函數(shù):
    public static void main(String[] args)//String是除了那8中以外的類型,所以是引用數(shù)組,一開始args并沒有被分配空間,它是用來接受命令行參數(shù)的。比如在命令行里輸入java calssname OtherWords,那么在args里就會保存OtherWords的內(nèi)容
    注:數(shù)組本身就是引用類型
 
  如何交換int x,int y的值,而不利用第三個變量:x=x+y;y=x-y;x=x-y;
 
  函數(shù)的調(diào)用:在Java中,傳參時,都是以傳值的方法進行。對于基本數(shù)據(jù)類型,傳遞的數(shù)組的拷貝;對于引用類型,傳遞的是引用的拷貝
 
  在打印(System.out.println)一個對象的時候,會自動調(diào)用這個對象的toString()方法,原Object類中的toString()方法Returns a string representation of the object.It is recommended that all subclasses override this method.
 
  對象的克隆:
    為了獲取對象的一份拷貝,我們可以利用Object類的clone()方法
    在派生類中覆蓋基類的clone()方法,并聲明為public(因為在Object類中該方法是protected)
    在派生類的clone()方法中,調(diào)用super.clone()。在運行時刻,Object中的clone()識別出你要復(fù)制的是哪一個對象,然后為此對象分配空間,并進行對象的復(fù)制,將原始對象的內(nèi)容一一復(fù)制到新對象的存儲空間中
    在派生類中實現(xiàn)Cloneable接口(沒有任何抽象方法,只是為了告訴編譯器這個對象可以克?。?,否則調(diào)用clone()方法會拋出CloneNotSupportedException異常
    如果被克隆的對象的數(shù)據(jù)成員中含有對象,那么就需要作深層次的克隆才能將作為數(shù)據(jù)成員的對象克隆,否則是把對象變量的值進行了拷貝,即只是把作為數(shù)據(jù)成員的對象的引用(地址)進行了拷貝。作深層次的克隆,就是要讓作為數(shù)據(jù)成員的對象所屬的類也覆蓋clone()方法,并且實現(xiàn)Cloneable接口(與被克隆的對象所屬的類中的實現(xiàn)方法同)
class Student implements Cloneable
{
      String name;
      int age;
      Student(String name,int age)
      {
             this.name=name;
             this.age=age;
      }
      public Object clone()
      {
             Object o=null;
             try
             {
             o=super.clone();//super.clone()返回的是Object類,根據(jù)需要可進行強制類型轉(zhuǎn)換
             }
             catch(CloneNotSupportedException e)
             {
                    System.out.println(e.toString());
             }
             return o;
      }
}

  封裝類:(有時函數(shù)參數(shù)是引用類型,但我們又要傳遞基本數(shù)據(jù)類型)
針對八種基本數(shù)據(jù)類型定義的相應(yīng)的引用類型——封裝類(在java.lang包中定義的,基本數(shù)據(jù)類型與封裝類建立對應(yīng)關(guān)系之后,封裝類就是只讀類,不能改變)
         基本數(shù)據(jù)類型                  封裝類
            boolean                    Boolean
             byte                       Byte
            short                       Short
             int                       Integer
             long                       Long
             char                     Character
             float                      Float
            double                     Double
--------------------------------------------------------------------
class Test
{
    public  static void main(String[] args)
    {
        int i=3;
        Integer in=new Integer(i);//Integer的一個構(gòu)造函數(shù)Integer(int value)
        int j=in.intValue();//int intValue()  Returns the value of this Integer as an int
        System.out.println("j="+j);
        String str=in.toString();//String toString() Returns a String object representing this Integer‘s value.
        System.out.println("str="+str);
        String str1="134";//String要是數(shù)字類型的
        System.out.println(Integer.valueOf(str1));//static Integer valueOf(String s) Returns an Integer object holding the value of the specified String.
        //static int parseInt(String s) Parses the string argument as a signed decimal integer. 
    }  
}

  Class類(在java.lang包中,Instances of the class Class represent classes and interfaces in a running Java application):
    在Java中,每個class都有一個相應(yīng)的Class對象。也就是說,當我們編寫一個類,編譯完成后,在生成的.class文件中,就會產(chǎn)生一個Class對象,用于表示這個類的類型信息
    獲取Class實例的三種方式:
      (1)利用對象調(diào)用getClass()方法獲取該對象的Class實例;
      (2)使用Class類的靜態(tài)方法forName(),用類的名字獲取一個Class實例(static Class forName(String className)  Returns the Class object associated with the class or interface with the given string name. );
      (3)運用.class的方式來獲取Class實例,對于基本數(shù)據(jù)類型的封裝類,還可以采用.TYPE來獲取相對應(yīng)的基本數(shù)據(jù)類型的Class實例
    在newInstance()調(diào)用類中缺省的構(gòu)造方法 Object newInstance()(可在不知該類的名字的時候,常見這個類的實例)  Creates a new instance of the class represented by this Class object.
    在運行期間,如果我們要產(chǎn)生某個類的對象,Java虛擬機(JVM)會檢查該類型的Class對象是否已被加載。如果沒有被加載,JVM會根據(jù)類的名稱找到.class文件并加載它。一旦某個類型的Class對象已被加載到內(nèi)存,就可以用它來產(chǎn)生該類型的所有對象
----------------------------------------------------------------------------------------------------
//獲取Class實例的三種方式:
class ClassTest
{
       public static void main(String[] args)
       {
             Point pt=new Point();
             Class c1=pt.getClass();
             System.out.println(c1.getName());// String getName() Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
            
             try
             {
                      Class c2=Class.forName("Point");//static Class forName(String className) throws ClassNotFoundException  Returns the Class object associated with the class or interface with the given string name.
                  System.out.println(c2.getName());
             }
             catch(Exception e)
             {
                   e.printStackTrace();
             }
             Class c3=Point.class;
             System.out.println(c3.getName());
            
             Class c4=int.class;
             System.out.println(c4.getName());
            
             Class c5=Integer.TYPE;
             System.out.println(c5.getName());
            
             Class c6=Integer.class;
             System.out.println(c6.getName());
            
       } 
}
class Point
{
   int x,y; 
}
/*結(jié)果:
Point
Point
Point
int
int
java.lang.Integer*/
----------------------------------------------------------------------------
//newInstance()調(diào)用類中缺省的構(gòu)造方法:
class anClassTest
{
       public static void main(String[] args)
       {
        if(args.length!=1)
        {
            return;
        }
        try
        {
            Class c=Class.forName(args[0]);
            Point pt=(Point)c.newInstance();
            pt.output();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
       }
}
class Point
{
   int x,y;
   void output()
   {
       System.out.println("x="+x+",y="+y); 
   } 
}
class Line
{
   int length,x,y;
   void output()
   {
       System.out.println("length="+length+",x="+x+",y="+y); 
   } 
}

?The Reflection API represents,or reflects,the classes,interfaces,and objects in the current Java Virtual Machine.在java.lang.reflect包里

  Runtime類(在java.lang包中定義)和Process類
    每一個Java程序都有一個Runtime類的單一實例
    通過Runtime.getRuntime()獲取Runtime類的實例
    Runtime類是使用單例模式的一個例子
import java.io.*;
class RuntimeTest
{
    public static void main(String[] args)
    {
         Runtime rt=Runtime.getRuntime();
         System.out.println(rt.freeMemory());//long freeMemory():Returns the amount of free memory in the Java Virtual Machine.
         System.out.println(rt.totalMemory());// long totalMemory():Returns the total amount of memory in the Java virtual machine.
         try
         {
                  rt.exec("notepad");//Process exec(String command):Executes the specified string command in a separate process.
                  Process p=rt.exec("java anClassTest Point");//現(xiàn)在p就表示java anClassTest這個子進程。類似這種可做圖形調(diào)用工具
                  InputStream is=p.getInputStream();
                  int data;
                  while((data=is.read())!=-1)
                  {
                       System.out.print((char)data); 
                  } 
         }
         catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

  設(shè)計模式:在我們進行程序設(shè)計時,逐漸形成了一些典型問題和問題的解決方案,這就是軟件模式。每一個模式描述了一個在我們程序設(shè)計中經(jīng)常發(fā)生的問題,以及該問題的解決方案。當我們碰到模式所描述的問題,就可以直接用相應(yīng)的解決方法去解決這個問題,這就是設(shè)計模式
  單例模式:
    (1)一個類只有一個實例,而且自行實例化并向這個系統(tǒng)提供這個實例,這個類成為單例類
    (2)單例類的一個重要的特點就是類的構(gòu)造方法是私有的,從而避免了外部利用構(gòu)造方法直接創(chuàng)造多個實例
  單例類的實現(xiàn)(比如設(shè)計計數(shù)器,還有Runtime這個類):
  class Singleton
  {
       private static final Singleton st=new Singleton();//因為為static final,所以在類加載的時候就構(gòu)造了這個實例
       private Singleton(){};
       public static Singleton getInstance()
       {
               reture st;
       }
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
java正確例題
Java基礎(chǔ)之:成員方法與傳參機制
XML 數(shù)據(jù)綁定框架 JiBX
神州數(shù)碼(中國)有限公司 JAVA高級軟件工程師(職位編號:50151142) 筆試題 |...
Java 2 實用教程(第5版)耿祥義版 習題八
小宋Java面經(jīng)
更多類似文章 >>
生活服務(wù)
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服