zoukankan      html  css  js  c++  java
  • {Java初级系列二}---Java类基础知识

    {Java初级系列二}---------Java类基础知识

    本人为自学Java系列,内容来自于中国大学mooc华东师范大学陈育良教授《Java核心技术》,在此感谢老师!

    一:Java类结构和main函数

    1. Java文件只能有一个public class
    2. Public class的名字还必须和文件名一致
    3. 文件可以有多个class,但是只能有一个是public。不提倡一个文件里面放着多个类(内部类除外)-------这个先记住,我们后面再贴代码理解
      1. public class IntegerTest {  
      2.      // PSVM 为函数入口  main函数是java程序入口
      3.     public static void main(String[] args) {  
      4.         int a = 5;  // 成员变量
      5.         int b = 6;  
      6.         int c = a+b;  
      7.         /* 同样可以写为 
      8.          * int a, b, c; 
      9.          * a=5;b=6; 
      10.          * c=a+b; 
      11.          *  
      12.          */  
      13.  
      14.         System.out.println(c);  
      15.         }     
      16. }  

      类是由两种构成

      1. 成员变量/属性
      2. 成员方法/函数

    Main函数:

    一个class只能有一个main函数,没有main函数的class只能被动被调用

    1. public class ArgumentTest {  
    2.     
    3.     public static void main(String[] args) {  
    4.         // TODO Auto-generated method stub  
    5.         for (int i=0; i<args.length;i++)  
    6.         {  
    7.             System.out.println(args[i]);  
    8.         }  
    9.     }  
    10.   }

    上图程序就是一个需要手工输入参数的java程序

    二:基本类型和运算符

    共计八种基本类型和若干运算符,这些也不需要记住,到实际应用场景实践即可

    三:选择结构和循环结构

    任何选择和循环结构我认为都可以理解为一个自定义函数,每个都是一个方法名,对应后面都是有方法体。

    选择结构:

    1. public class Test123 {  
    2.     
    3.     public static void main(String[] args) {  
    4.         // TODO Auto-generated method stub  
    5.     int a = 10;  
    6.     if (a>10) {  
    7.         System.out.println("aaaaaa");  
    8.     }  
    9.     if(a>11) {  
    10.         System.out.println("bbbbbb");  
    11.     }  
    12.     else   
    13.     {System.out.println("cccc");      
    14.     }  
    15.     if (a>=5) {  
    16.         System.out.println("dddd");  
    17.     }  
    18.     else if (a<=5){  
    19.         System.out.println("fffffff");  
    20.     }  
    21.     else   
    22.     {System.out.println("eeeee");}  
    23.     }  
    24. }  

    输出结果:

    Switch选择结构:

    Case就可以满足多个分支:

    每个case后面都要加上break来中断,最后一个分支为break。最后一个分支为default

    1. public class test1234 {  
    2.     public static void main(String[] args) {  
    3.     int a =1;  
    4.     int b =2;  
    5.     switch(a+b) {  
    6.     case 1 : System.out.println("aaaaaa");  
    7.     break;  
    8.     case 2 : System.out.println("bbbbb");  
    9.      break;  
    10.     case 3 : System.out.println("cccccc");  
    11.     break;  
    12.     default:System.out.println("dddddd");   
    13.     }  
    14.     String a3  = "abc";  
    15.     switch (a3) {  
    16.     case "abc":System.out.println("eeeee");  
    17.     case "def":System.out.println("fffff");  
    18.     case "ghj":System.out.println("ggggg"); }  
    19.     }  
    20. }  

    循环结构:

    While和do while两种循环体区别在于:    while是先判断后循环,判断条件满足才会执行循环体内语句;而do-while是先执行循环中的语句,然后再判断表达式是否满足,如果为假,就终止循环

    1. public class TEst12345 {  
    2.     
    3.     public static void main(String[] args) {  
    4.         // TODO Auto-generated method stub  
    5.        System.out.println("============While Test============");  
    6.        int x = 10;  
    7.        while(x<20) {System.out.print("value of x is: "+x) ;  
    8.        x++;  
    9.        System.out.println(" ");  
    10.        }  
    11.        System.out.println("====Do while Test=========");  
    12.        int x1 =10;  
    13.        do {  
    14.            x1++;  
    15.            // continue  结束当前循环,继续下次循环。当前余下的循环余下的代码不做  
    16.            if(x1%2==0) {continue;}  
    17.            System.out.println("value of x is: "+x1);  
    18.        }while(x1<20);  
    19.     }  
    20. }

    输出结果如下:

    1. ============While Test============  
    2. value of x is: 10  
    3. value of x is: 11  
    4. value of x is: 12  
    5. value of x is: 13  
    6. value of x is: 14  
    7. value of x is: 15  
    8. value of x is: 16  
    9. value of x is: 17  
    10. value of x is: 18  
    11. value of x is: 19  
    12. ====Do while Test=========  
    13. value of x is: 11  
    14. value of x is: 13  
    15. value of x is: 15  
    16. value of x is: 17  
    17. value of x is: 19  

    For循环

    1. public class Fortest {  
    2.    public static void main(String[] args) {  
    3.         // TODO Auto-generated method stub  
    4.         for(int i=0;i<4;i++) {  
    5.             for(int j=0;j<5;j++) {  
    6.             if(j<=i) {  
    7.                     System.out.print("*");}  
    8.             else {break;}  
    9.             }  
    10.             System.out.println();  
    11.         }  
    12.     }  
    13. }  

    输出结果:

    1. *  
    2. **  
    3. ***  
    4. **** 

    四:自定义函数

    自定义函数:

    修饰词(public+static)+返回值(int或者void),函数名(形参列表){函数体;}

    代码实例:

    如图是求5的阶乘的代码,自定义factorialCalculation函数是自定义函数,在main函数中可以进行调用

    1. public class FactorialTest {  
    2.       public static void main(String[] args) {  
    3.         // TODO Auto-generated method stub  
    4.           int a = 5;  
    5.           int b =FactorialTest.factorialCalculation(a);  
    6.          System.out.println("The factorial of a is: "+ b);  
    7.     }  
    8.     public static int factorialCalculation(int m) {  
    9.         if (m>1) {  
    10.         return m*factorialCalculation(m-1);  
    11.                 }  
    12.         else {  
    13.             return 1;  
    14.         }  
    15.     }  
    16.     
    17. }  

    重载-----这个概念后面会经常接触到

    • 同一个类中,函数名称可以相同,即重载函数。但是函数参数的个数或者类型必须有所不同
    • 不能以返回值来却分同名的函数
    • public class OverLoad {  
    •     
    •     public static void main(String[] args) {  
    •         // TODO Auto-generated method stub  
    •         System.out.println(add(1,2));  
    •         System.out.println(add(1.5,2.5));  
    •     }  
    • public static double add(int m,int n) {  
    •     return (m+n);//低精度转为高精度 无须转型 
    • }  
    • public static int  add(double c,double d)  
    • {  
    •     return (int) (c+d);}//此处将double转为int,高精度转为低精度需要转型  
    • }  
  • 相关阅读:
    Win10 ntoskrnl.exe蓝屏解决
    Log POST Data in Nginx
    MACOS关闭指定端口
    获取Skype用户IP地址
    禁止windows10带来的三大隐患问题
    各种语言一句话反弹shell
    2015阿里巴巴安全峰会PPT
    HTTPS反向代理嗅探
    利用arpspoof和urlsnarf 进行ARP嗅探
    收集的几个存在漏洞的程序
  • 原文地址:https://www.cnblogs.com/yblecs/p/12227129.html
Copyright © 2011-2022 走看看