JAVA-day03-物件導向start,java-day03-start

來源:互聯網
上載者:User

JAVA-day03-物件導向start,java-day03-start

//十進位轉二進位class  Demo1{public static void main(String[] args) {int num =6;int[] arr = new int[32];int index =0;while(num!=0){arr[index++] = num%2;num = num/2;}       index--;       for(;index>=0;index--){System.out.print(arr[index]);    }}}//十進位轉十六進位class  Demo2{public static void main(String[] args) {toHex2(800);//00000000 00000000 00000000 0000000        0011       1100}//十進位轉十六進位//結果:void//參數:一個數public static void toHex1(int num){int[] arr = new int[8];int index = arr.length;//for(int i=1;i<=8;i++)while(num!=0){arr[--index] = num&15;num =  num>>>4;}for(int i=index;i<arr.length;i++){if(arr[i]>9)   System.out.print((char)(arr[i]-10+'a'));else System.out.print(arr[i]);}}    //十進位轉十六進位---查表法public static void toHex2(int num){char[] ch={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};char[] arr = new char[8];int index = arr.length;while(num!=0){   int n =  num&15;   arr[--index] = ch[n];   num = num>>>4;}for(int i = index;i<arr.length;i++){System.out.print(arr[i]);}}////十進位轉二進位---查表法public static void toBinary2(int num){char[] ch={'0','1'};char[] arr = new char[32];int index = arr.length;while(num!=0){   int n =  num&1;   arr[--index] = ch[n];   num = num>>>1;}for(int i = index;i<arr.length;i++){System.out.print(arr[i]);}}////十進位轉八進位---查表法public static void toOctal2(int num){char[] ch={'0','1','2','3','4','5','6','7'};char[] arr = new char[11];int index = arr.length;while(num!=0){   int n =  num&7;   arr[--index] = ch[n];   num = num>>>3;}for(int i = index;i<arr.length;i++){System.out.print(arr[i]);}}//進位轉換的通用功能public static void toAny(int num,int base,int offset){char[] ch={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};char[] arr = new char[32];int index = arr.length;while(num!=0){   int n =  num&base;   arr[--index] = ch[n];   num = num>>>offset;}for(int i = index;i<arr.length;i++){System.out.print(arr[i]);}}public static void toBinary(int num){toAny(num,1,1);}public static void toOctal(int num){toAny(num,7,3);}public static void toHex(int num){toAny(num,15,4);}}import java.util.Arrays;class Demo3 {public static void main(String[] args) {int[] arr = {3,45,67,89,123,456};int key = 50;//int index = half(arr,key);int index = Arrays.binarySearch(arr,key);System.out.println("index="+index);}//對於一個有序的數組,如果要插入一個元素並保證數組還有序,問如何擷取該元素位置。public static int half(int[] arr,int key){int min = 0,max = arr.length-1,mid;while(min<=max){mid = (min+max)>>1;if(key>arr[mid])min = mid+1;else if(key<arr[mid])max = mid -1;elsereturn mid;}return  -min-1;}//1,將給定數組進行反轉。{32,65,12,89,41} {41,89,12,65,32}public static void fanZhuan(int[] arr){for(int i=0,j=arr.length-1;i<j;i++,j--){huan(arr,i,j);}}public static void huan(int[] arr,int i,int j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}//二維數組class  Demo4{public static void main(String[] args) {int[][] arr = new int[2][3];        for(int i=0;i<arr.length;i++){for(int j=0;j<arr[i].length;j++)System.out.print(arr[i][j]);            System.out.println();}int[][] a ={{1,2,3,4},{7,8,9},{1,1,1,1,1,1}};for(int i=0;i<a.length;i++){for(int j=0;j<a[i].length;j++)System.out.print(a[i][j]);            System.out.println();}int[][] b = new int[2][];        b[0] = new int[5];b[1] = new int[3];        }}class Car{int num;String color;public void run(){System.out.println("run");}}//在類中定義的變數:成員變數,在類中定義的函數:成員方法class Demo5{public static void main(String[] args) {Car car = new Car();//使用Car.class建立了一個Car類型的對象car.num = 4;car.color ="紅色";car.run();}}



/*成員變數和局部變數的對比:1:成員變數有預設值   局部變數沒有預設值2:成員變數的範圍是整個類   局部變數的範圍是從它被定義的位置開始到它所在的大括弧結束3:成員變數存在於堆中   局部變數存在於棧中4:成員變數是隨著對象的出現而出現,隨著對象的被記憶體回收而消失   局部變數是隨著其所在函數被調用才入棧,隨著它的範圍的結束而從棧中消失*/class Car{int num;//成員變數String color;public void run(){System.out.println(num+","+color);}}class Demo6{public static void main(String[] args) {int num = 23;Car car = new Car();System.out.println(car.num);System.out.println(car.color);car.run();//匿名對象:當一個對象只需要被使用一次時,可以考慮匿名對象new Car().num = 5;new Car().color="red";//這句代碼執行完,這個對象就成了垃圾        //Car c = new Car();        show(new Car());//在傳遞參數是使用匿名對象,可以簡化書寫}public static void show(Car car){car.run();}}/*封裝,繼承,多態建構函式:是用來建立對象的,如果一個類中沒有建構函式,那麼系統會自動在類中加入一個預設的建構函式,類名(){}          如果自己在類中寫了建構函式,那麼預設的建構函式就不存在了建構函式的特點:可用於對象的初始化1:必須和類名相同2:不能有傳回值類型3:不能被調用,只能在建立對象時使用*/class Person{String name;int age;Person(){}Person(String ming,int nian){name = ming;age = nian;}public void speak(){System.out.println("人會說話");}}class  Demo7{public static void main(String[] args) {Person ren = new Person("李白",238);}}



//封裝:只對外提供有用的屬性和方法class MyMath{private static void toAny(int num,int base,int offset){char[] ch={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};char[] arr = new char[32];int index = arr.length;while(num!=0){   int n =  num&base;   arr[--index] = ch[n];   num = num>>>offset;}for(int i = index;i<arr.length;i++){System.out.print(arr[i]);}}public static void toBinary(int num){toAny(num,1,1);}public static void toOctal(int num){toAny(num,7,3);}public static void toHex(int num){toAny(num,15,4);}}class Demo8 {public static void main(String[] args) {MyMath  mm = new MyMath();}}class Student{private String name;private int age;public Student(String ming,int nian){name = ming;age = nian;}public void setAge(int nian){if(nian<18 || nian>38)System.out.println("你的年齡不合適");elseage = nian;}public int getAge(){return age;}}class Demo9 {public static void main(String[] args) {Student ren = new Student("小黑",20);        ren.setAge(22);System.out.println(ren.getAge());}}//this:是一個引用,總是指向當前正被使用的對象class Student{String name;int age;public Student(){}public Student(String name,int age)//當局部變數和成員變數同名時,成員變數無效{this.name = name;this.age = age;}public void show(){System.out.println(name+","+age);}//比較兩個學員是否是同齡人//結果:boolean//參數:一個學員public boolean isSameAge(Student stu){return this.age==stu.age;}}class Demo10 {public static void main(String[] args) {Student stu = new Student("木樁",22);//System.out.println(stu.name+","+stu.age);stu.show();Student stu2 = new Student("木樁2",22);//System.out.println(stu2.name+","+stu2.age);       boolean flag = stu.isSameAge(stu2);   System.out.println(flag);}}//this:可以用於建構函式之間的調用,這個函數調用必須寫在建構函式的第一行class Student{String name;int age;public Student(){this("lisi",20);}public Student(String name,int age){//this();this.name = name;this.age = age;}}class Demo11{public static void main(String[] args) {Student stu = new Student("木樁",22);}}/*static:修飾符,可以修飾成員變數,成員函數1:被靜態修飾的成員變數,是其所屬類的所有對象共用2:靜態是隨著類的載入就載入了,所以是優先於的對象的存在3:靜態又多了一種調用方式,類名.靜態成員弊端:生命週期太長靜態成員變數和非靜態成員變數的對比:1:儲存位置靜態成員變數儲存在方法區的靜態區中非靜態成員變數儲存在堆中2:生命週期靜態成員變數隨著類的載入就在靜態區中開闢了記憶體,隨著其所屬class的消失才消失非靜態成員變數隨著對象的建立而存在,隨著對象的被記憶體回收就消失3:儲存資料的特點靜態成員變數儲存的是所有對象共用的資料非靜態成員變數儲存的是每個對象特有的資料4:調用方式靜態成員變數既可以通過對象調用也可以通過類名調用非靜態成員變數只能通過對象調用*/class Person{String name;//執行個體變數static String country ="CN";//類變數    //非靜態既可以使用靜態,也可以使用非靜態public void show()//執行個體方法{System.out.println(country);}    //靜態只能使用靜態public static void fun()//類方法{System.out.println(country);}}class Demo12 {public static void main(String[] args) {Person ren = new Person();ren.name = "lisi";/*System.out.println(ren.country);System.out.println(Person.country);ren.country="USA";*/Person  ren2 = new Person();//System.out.println(ren2.country);ren2.fun();Person.fun();}}/*public static void main(String[] args)public :說明存取權限是最大的static:說明該函數是隨著類的載入就載入了void:表示無傳回值main:一個函數名,不是關鍵字,只是被JVM所識別String[] args:一個字串類型的數組,參數*/class Test{public static void main(String[] args){String[] arr ={"woieuro","eurioew","ldkjfgkldf"};        Demo13.main(arr);}}class Demo13 {public static void main(String[] args) {//System.out.println(args);//[Ljava.lang.String;@949f69//System.out.println(args.length);for(int i=0;i<args.length;i++){System.out.println(args[i]);}main(10);}public static void main(int a)//重載{System.out.println(a);}}/*什麼時候用靜態:成員變數:該成員變數的值可以被其所屬類的所有對象共用成員函數:該成員函數沒有用到其所屬類中的任何非靜態成員*/class Person{String name;Person(String name){this.name = name;}public static void fun(Person per)//可以靜態,沒有用到Person類中的非靜態成員{System.out.println(per.name);}}class Demo14 {public static void main(String[] args) {Person p1 = new Person("zhangsan");Person p2 = new Person("lisi");p1.fun(p2);}}class Demo15 {public static void main(String[] args) {int[] arr={23,4,67,78,9};ArrayTool  arrayTool = new ArrayTool();//ArrayTool.select(arr);//ArrayTool.print(arr);        arrayTool.select(arr);arrayTool.print(arr);}}/**這是一個定義了常用的運算元組的功能的工具類,比如求最值,排序,尋找等等@author 小黑@version V1.0*/public class  ArrayTool{//把構造方法私人化    private ArrayTool(){}    /**這是求數組中的最大值的方法    @param 接收一個整型的數組@return 返回數組中的最大值*/public static  int getMax(int[] arr){int max = arr[0];for(int i=0;i<arr.length;i++){if(arr[i]>max)max = arr[i];}return max;}/**這是求數組中的最小值的方法    @param 接收一個整型的數組@return 返回數組中的最小值*/public static  int getMin(int[] arr){int min = arr[0];for(int i=0;i<arr.length;i++){if(arr[i]<min)min = arr[i];}return min;}    /**這是對數組進行選擇排序的方法    @param 接收一個整型的數組@return 無傳回值*/public static void select(int[] arr){for(int i=0;i<arr.length-1;i++){for(int j=i+1;j<arr.length;j++){if(arr[j]<arr[i]){huan(arr,i,j);}}}}/**這是對數組中指定下標中的值進行交換的方法    @param 接收一個整型的數組@param i 接收一個下標@param j 接收一個下標*/public static void huan(int[] arr,int i,int j){int temp;temp = arr[i];arr[i] = arr[j];arr[j] = temp;}    /**這是從數組中尋找某一個數的方法    @param 接收一個整型的數組@param 被尋找的數    @return 返回被尋找的數的下標,沒找到返回-1*/public static int halfSearch(int[] arr,int key){int min = 0,max = arr.length-1,mid;while(min<=max){mid = (min+max)>>1;if(key>arr[mid])min = mid +1;else if(key<arr[mid])max = mid -1;else return mid;}return -1;}    /**這是列印數組中的值的方法    @param 接收一個整型的數組@return 無傳回值*/public static void print(int[] arr){for(int i=0;i<arr.length;i++){if(i!=arr.length-1)System.out.print(arr[i]+",");else System.out.print(arr[i]);}}}/*靜態代碼塊:隨著類的載入而執行,只執行一次,優先於main方法的執行static{}*/class Test{    static int num =6;//需要寫在靜態代碼塊的前邊兒static{System.out.println(num);}public static void show(){System.out.println(num);}}class Demo16 {static{System.out.println("b");}public static void main(String[] args) {//Test t = new Test();//Test t2 = new Test();//每個class只被載入一次//System.out.println("d");Test.show();//Test test = null;//Test.class沒有被載入}static{System.out.println("c");}}/*構造代碼塊:對象一建立就被執行,優先於建構函式的執行,可用於對所有對象的初始化和建構函式的區別:建構函式只是針對某個對象進行初始化{}*/class Person{String name ="哈哈";String country;static{System.out.println("a");}Person(){}{  System.out.println(this.name);}Person(String name){this.name = name;        System.out.println(this.name);}public void show(){System.out.println(this.name);}}class Demo17 {static{System.out.println("b");}public static void main(String[] args) {Person ren1 = new Person("呵呵");        //局部代碼塊:可以限定變數的範圍{   int a = 56;   System.out.println(a);}    }}/*對象的初始化過程:1:因為建立對象要使用class,,所以先載入相應類的位元組碼檔案 類名.class2:如果有靜態代碼塊,執行靜態代碼塊3:在對記憶體中開闢空間4:對堆記憶體中的成員變數進行預設初始化(賦預設值)5: 對堆記憶體中的成員變數進行顯示初始化6:執行構造代碼塊7:執行建構函式8:將堆記憶體中的記憶體首地址賦給棧中的引用*/class Person{private String name="xiaohong";private int age=23;private static String country="CN";{System.out.println(name+"  "+age);}public Person(String name,int age){this.name = name;this.age = age;}public void setName(String name){   this.name = name;}public String getName(){return this.name;}}class  Demo18{public static void main(String[] args) {Person p = new Person("小白",20);p.setName("小黑");}}練習:註:按Java規範書寫程式碼,如果你認為程式有錯誤,請指出,並說明程式錯誤原因。1.class Demo{void show(int a,int b,float c){}}A.void show(int a,float c,int b){}//B,void show(int a,int b,float c){}//C.int show(int a,float c,int b){return a;}//D.int show(int a,float c){return a;}//哪個答案和show函數重載。ACD--------------------------------------------------2.寫出結果。class Demo{public static void main(String[] args){int x=0,y=1;if(++x==y--&x++==1||--y==0)/System.out.println("x="+x+",y="+y);//elseSystem.out.println("y="+y+",x="+x);}}x=2,y=0--------------------------------------------------3.寫出輸出結果。class Demo{public static void main(String[] args){int a=3,b=8;int c=(a>b)?a++:b++;System.out.println("a="+a+"\tb="+b+"\tc="+c);  int d=(a>b)?++a:++b;System.out.println("a="+a+"\tb="+b+"\td="+d);  int e=(a<b)?a++:b++;System.out.println("a="+a+"\tb="+b+"\te="+e); int f=(a<b)?++a:++b;System.out.println("a="+a+"\tb="+b+"\tf="+f);  }}a=3,b=9,c=8    a=3,b=10,d=9  a=4,b=10,e=3  a=5,b=10,f=5--------------------------------------------------4.寫出結果。class Demo{public static void main(String[] args){ int m=0,n=3;if(m>0){    if(n>2)System.out.println("A");     elseSystem.out.println("B");}}}--------------------------------------------------5.寫出結果。public class Demo{ public static void main(String []args){ int i = 0, j = 5; tp: for (;;){ i++; for(;;){if(i > j--)break tp; }} System.out.println("i = " + i + ", j = "+ j);} } i= 1,j=-1--------------------------------------------------6.寫出結果。class Demo      {public static void main(String[] args){String foo="blue"; boolean[] bar=new boolean[2]; if(bar[0]){       foo="green";    } System.out.println(foo);}}blue--------------------------------------------------7.寫出結果。public class Test      { public static void leftshift(int i, int j){    i+=j; } public static void main(String args[]){ int i = 4, j = 2; leftshift(i, j); System.out.println(i); } } i=4--------------------------------------------------8.寫出結果。public class Demo{ public static void main(String[] args){ int[] a=new int[1]; modify(a); System.out.println(a[0]); }public static void modify(int[] a){ a[0]++;} } 1--------------------------------------------------9.class Test{ public static void main(String[] args){ String foo=args[1]; String bar=args[2]; String baz=args[3]; } } d:\>java Test Red Green Blue what is the value of baz?   A. baz has value of ""   B. baz has value of null   C. baz has value of "Red"   D. baz has value of "Blue"   E. baz has value of "Green"   F. the code does not compile   G. the program throw an exception G--------------------------------------------------11.簡述:函數在程式中的作用以及運行特點。。實現代碼的重複利用。函數被調用則進棧,使用結束出棧--------------------------------------------------12.列印99乘法表public class foo{  public static void main(String[]args){   for(int i =1;i<=9;i++){    for(int j =1;j<=i;j++){     System.out.print(j+"*"+i+j*i+" ");      }     System.out.println();    }   }}--------------------------------------------------13.列印下列圖形    *   * *  * * * * * * ** * * * * public class foo{  public static void main(String[]args){    for(int i = 1;i<=5;i++){       for(int j =1;j<=j-i;j++){         System.out.print(" ");          }      for(int k =1;k<=i;k++){         System.out.println("*");           }         System.out.println();        }      }   }------------------------------------------------------14.下面哪個數組定義是錯誤的。並對錯誤的答案加上單行注釋,寫出錯誤的原因。A,float[]=new float[3]; //沒有引用名B, float f2[]=new float[];//沒有定義長度C, float[] f1=new float[3];D, boolean[] b={"true","false","true"};//類型不一致E, double f4[]={1,3,5}; F, int f5[]=new int[3]{2,3,4};  //不能定義長度G, float f4[]={1.2,3.0,5.4};//必須在數值後面加上f


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.