标签:black 中断 java 文件 阶乘 属性 *** add ffffff
{Java初级系列二}---------Java类基础知识
本人为自学Java系列,内容来自于中国大学mooc华东师范大学陈育良教授《Java核心技术》,在此感谢老师!
一:Java类结构和main函数
Java文件只能有一个public class
Public class的名字还必须和文件名一致
文件可以有多个class,但是只能有一个是public。不提倡一个文件里面放着多个类(内部类除外)-------这个先记住,我们后面再贴代码理解
public?class?IntegerTest?{??
?????//?PSVM?为函数入口??main函数是java程序入口
????public?static?void?main(String[]?args)?{??
????????int?a?=?5;??// 成员变量
????????int?b?=?6;??
????????int?c?=?a+b;??
????????/*?同样可以写为?
?????????*?int?a,?b,?c;?
?????????*?a=5;b=6;?
?????????*?c=a+b;?
?????????*??
?????????*/??
?
????????System.out.println(c);??
????????}?????
}??
类是由两种构成
成员变量/属性
成员方法/函数
Main函数:
一个class只能有一个main函数,没有main函数的class只能被动被调用
public?class?ArgumentTest?{??
??
?
????public?static?void?main(String[]?args)?{??
????????//?TODO?Auto-generated?method?stub??
????????for?(int?i=0;?i<args.length;i++)??
????????{??
????????????System.out.println(args[i]);??
????????}??
????}??
??}
上图程序就是一个需要手工输入参数的java程序
二:基本类型和运算符
共计八种基本类型和若干运算符,这些也不需要记住,到实际应用场景实践即可
三:选择结构和循环结构
任何选择和循环结构我认为都可以理解为一个自定义函数,每个都是一个方法名,对应后面都是有方法体。
选择结构:
public?class?Test123?{??
??
?
????public?static?void?main(String[]?args)?{??
????????//?TODO?Auto-generated?method?stub??
????int?a?=?10;??
????if?(a>10)?{??
????????System.out.println("aaaaaa");??
????}??
????if(a>11)?{??
????????System.out.println("bbbbbb");??
????}??
????else???
????{System.out.println("cccc");??????
????}??
????if?(a>=5)?{??
????????System.out.println("dddd");??
????}??
????else?if?(a<=5){??
????????System.out.println("fffffff");??
????}??
????else???
????{System.out.println("eeeee");}??
????}??
}??
输出结果:
Switch选择结构:
Case就可以满足多个分支:
每个case后面都要加上break来中断,最后一个分支为break。最后一个分支为default
public?class?test1234?{??
????public?static?void?main(String[]?args)?{??
????int?a?=1;??
????int?b?=2;??
????switch(a+b)?{??
????case?1?:?System.out.println("aaaaaa");??
????break;??
????case?2?:?System.out.println("bbbbb");??
?????break;??
????case?3?:?System.out.println("cccccc");??
????break;??
????default:System.out.println("dddddd");???
????}??
????String?a3??=?"abc";??
????switch?(a3)?{??
????case?"abc":System.out.println("eeeee");??
????case?"def":System.out.println("fffff");??
????case?"ghj":System.out.println("ggggg");?}??
????}??
}??
循环结构:
While和do while两种循环体区别在于:????while是先判断后循环,判断条件满足才会执行循环体内语句;而do-while是先执行循环中的语句,然后再判断表达式是否满足,如果为假,就终止循环
public?class?TEst12345?{??
??
?
????public?static?void?main(String[]?args)?{??
????????//?TODO?Auto-generated?method?stub??
???????System.out.println("============While?Test============");??
???????int?x?=?10;??
???????while(x<20)?{System.out.print("value?of?x?is:?"+x)?;??
???????x++;??
???????System.out.println("\n");??
???????}??
???????System.out.println("====Do?while?Test=========");??
???????int?x1?=10;??
???????do?{??
???????????x1++;??
???????????//?continue??结束当前循环,继续下次循环。当前余下的循环余下的代码不做??
???????????if(x1%2==0)?{continue;}??
???????????System.out.println("value?of?x?is:?"+x1);??
???????}while(x1<20);??
????}??
}
输出结果如下:
============While?Test============??
value?of?x?is:?10??
value?of?x?is:?11??
value?of?x?is:?12??
value?of?x?is:?13??
value?of?x?is:?14??
value?of?x?is:?15??
value?of?x?is:?16??
value?of?x?is:?17??
value?of?x?is:?18??
value?of?x?is:?19??
====Do?while?Test=========??
value?of?x?is:?11??
value?of?x?is:?13??
value?of?x?is:?15??
value?of?x?is:?17??
value?of?x?is:?19??
For循环
public?class?Fortest?{??
???public?static?void?main(String[]?args)?{??
????????//?TODO?Auto-generated?method?stub??
????????for(int?i=0;i<4;i++)?{??
????????????for(int?j=0;j<5;j++)?{??
????????????if(j<=i)?{??
????????????????????System.out.print("*");}??
????????????else?{break;}??
????????????}??
????????????System.out.println();??
????????}??
????}??
}??
输出结果:
*??
**??
***??
****?
四:自定义函数
自定义函数:
修饰词(public+static)+返回值(int或者void),函数名(形参列表){函数体;}
代码实例:
如图是求5的阶乘的代码,自定义factorialCalculation函数是自定义函数,在main函数中可以进行调用
public?class?FactorialTest?{??
??????public?static?void?main(String[]?args)?{??
????????//?TODO?Auto-generated?method?stub??
??????????int?a?=?5;??
??????????int?b?=FactorialTest.factorialCalculation(a);??
?????????System.out.println("The?factorial?of?a?is:?"+?b);??
????}??
????public?static?int?factorialCalculation(int?m)?{??
????????if?(m>1)?{??
????????return?m*factorialCalculation(m-1);??
????????????????}??
????????else?{??
????????????return?1;??
????????}??
????}??
??
?
}??
?
重载-----这个概念后面会经常接触到
同一个类中,函数名称可以相同,即重载函数。但是函数参数的个数或者类型必须有所不同
不能以返回值来却分同名的函数
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,高精度转为低精度需要转型??
}??
{Java初级系列二}---------Java类基础知识
标签:black 中断 java 文件 阶乘 属性 *** add ffffff
原文地址:https://www.cnblogs.com/yblecs/p/12227129.html