• Java入门——面向对象基础


    Java入门——面向对象基础



    本博客目的

    1. 练习题(重要)
    2. 面向对象的概念(了解)
    3. 面向对象的三大特性(重要)

    2016-09-10——13:13:39


    ll练习题


     用方法调用的形式进行数组排序

     1 package Sep10;
     2 //调用方法完成两个整形数组的排序并打印
     3 public class ArrayRefDemo03 {
     4 
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         // TODO Auto-generated method stub
    10         int age[]={12,32,43,54,43};
    11         int score[]={123,312,432,54,65};
    12         sort(age);
    13         print(age);
    14         System.out.println("
    ************************************");
    15         sort(score);
    16         print(score);
    17     }
    18     public static void sort(int x[]){
    19         for(int i=0;i<x.length;i++){
    20             for(int j=i+1;j<x.length;j++){
    21                 if(x[i]>x[j]){
    22                     int middle=x[i];
    23                     x[i]=x[j];
    24                     x[j]=middle;
    25                 }
    26             }
    27         }
    28         
    29     }
    30     public static void print(int temp[]){
    31         for(int i=0;i<temp.length;i++){
    32             System.out.print(temp[i]+"	");
    33         }
    34     }
    35 }
    12    32    43    43    54    
    ************************************
    54    65    123    312    432    

    使用Java类库进行数组排序

     1 package Sep10;
     2 import java.util.Arrays;
     3 public class ArrayRefDemo04 {
     4 
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         // TODO Auto-generated method stub
    10         int age[]={12,32,43,54,43};
    11         int score[]={123,312,432,54,65};
    12         Arrays.sort(age);//Java类库中自带的Array类中本身有排序函数
    13         print(age);
    14         System.out.println("
    ************************************");
    15         Arrays.sort(score);
    16         print(score);
    17     }
    18     public static void print(int temp[]){
    19         for(int i=0;i<temp.length;i++){
    20             System.out.print(temp[i]+"	");
    21         }
    22     }
    23 }
    12    32    43    43    54    
    ************************************
    54    65    123    312    432     

    编写求1!+2!+3!+......+30!并显示结果的程序

     1 package Sep10;
     2 //编写求1!+2!+3!+......+30!并显示结果的程序!
     3 public class Demo01 {
     4 
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         // TODO Auto-generated method stub
    10         long y=0;
    11         for(int i=1;i<=30;i++){
    12             y=y+factorSeries(i);
    13         }
    14         System.out.println(y);
    15     }
    16     //求阶乘的函数
    17     public static int factorSeries(int x){
    18         int j=x;
    19         while(x>1){
    20             j=j*(x-1);
    21             x--;
    22         }
    23         return j;
    24     }
    25 }
    3430808089

    面向对象的概念 

     1 package Sep10;
     2 //对象的创建于使用
     3 public class ClassDemo02 {
     4 
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         // TODO Auto-generated method stub
    10         Person per=new Person("Bob",23);
    11         per.info();
    12     }
    13 
    14 }
    15 class Person{
    16     String name;
    17     int age;
    18     public Person(String name,int age){
    19         this.name=name;
    20         this.age=age;
    21     }
    22     public void info(){
    23         System.out.println("姓名"+name+",年龄"+age);
    24     }
    25 }
    姓名Bob,年龄23

    对象引用传递

     1 package Sep10;
     2 //对象引用传递其实就是把等号后边的对象的栈内存空间交给等号前面的使用
     3 public class CalssDemo05 {
     4 
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         // TODO Auto-generated method stub
    10         Person2 per1=new Person2();
    11         Person2 per2=new Person2();
    12         per1.age=21;
    13         per1.name="Bob";
    14         per2=per1;
    15         per2.age=23;
    16         per2.name="glove";
    17         per1.info();
    18         per2.info();
    19     }
    20 }
    21 class Person2{
    22     String name;
    23     int age;
    24     public void info(){
    25         System.out.println("姓名:"+name+",年龄"+age);
    26     }
    27 }
    姓名:glove,年龄23
    姓名:glove,年龄23

    封装性

    在开发中一般将类的属性封装,属性是私有的,无法通过对象进行访问,必须通过setter和getter方法获得,可以在setter处加上“检验代码”以确保私有变量的现实意义。而通过setter赋值显然是一种非常麻烦的方法,所以一般通过构造方法初始化对象。


    类设计


    String类

      可以直接用“实例化”,也可以用new String(“”),无法直接用'=='来比较字符串的内容,需要用到equals方法——str1.euqals(str2);返回一个boolean量。

      String对象内容的改变实际上是通过内存地址的“断开-连接”实现的,而本身字符串中的内容并没有任何改变。

  • 相关阅读:
    常用命令收集
    wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容)
    Wireshark找不到网络接口问题
    带卡扣的网卡接口使用小Tips,大家注意插拔网线的手法啊!
    HOW TO BE SINGLE 最后那段的摘录
    github常见操作和常见错误!错误提示:fatal: remote origin already exists.
    关于工伤事故索赔计算很好用的一款APP
    MySQL文件目录格式及存放位置
    如何取消win10电脑自动更新
    推荐!手把手教你使用Git
  • 原文地址:https://www.cnblogs.com/BoscoGuo/p/5859395.html
Copyright © 2020-2023  润新知