JAVA-day03-面向对象start

来源:互联网 发布:武汉鲨鱼网络直播 编辑:程序博客网 时间:2024/06/10 07:45
//十进制转二进制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


1 0
原创粉丝点击