/**
- 變量與運(yùn)算符
- 標(biāo)識(shí)符:就是一個(gè)名稱 例如類名:VariableDemo
- 特點(diǎn)(規(guī)則):首字母大寫
- 關(guān)鍵字:是具有特殊意義的單詞 例如:public(公有的) class(類) static(靜態(tài)的) void(無(wú)返回值的)
- 特點(diǎn):都是小寫的
- 注意:包名全都小寫 格式com.neusoft
- 變量:變量是指在程序的運(yùn)行過(guò)程中隨時(shí)可以發(fā)生變化的量
- 作用:保存數(shù)據(jù) 例如:x = 0 y = 20
- Java中數(shù)據(jù)類型:
有兩種數(shù)據(jù)類型:1.基本類型(8種):整型(byte 8位 、short 16位、 int 32位、 long 64位) 最常用的默認(rèn)的類型為 int類型 整型的默認(rèn)值為0浮點(diǎn)型(float 32位 double 64位) 浮點(diǎn)數(shù)默認(rèn)的類型為double類型 浮點(diǎn)型默認(rèn)值為0.0布爾(boolean):只有兩個(gè)值(true/false) 布爾(boolean)默認(rèn)值為false字符(char):表示鍵盤上一個(gè)特定的字符 使用'一個(gè)特定的字符' 注意與"多個(gè)字符組成的字符串"2.引用類型- 如何定義一個(gè)變量呢?語(yǔ)法:數(shù)據(jù)類型 變量名[=變量的初始值];
- 注意:變量一定要先初始化然后才能使用
- @author ttc
*/
public class VariableDemo{
public static void main(String[] args) {
String name="tom";
byte age = 100;//一個(gè)字節(jié)=8位
short number = 45;//16位
int mon=1000;//32位
long nuk = 1133L;//64位
float f = 23.567F;
double d = 3.4;
boolean isRight = false;
char c = 'a';
System.out.println(c);
System.out.println(isRight);
System.out.println(f);
System.out.println(d);
System.out.println(34.567);//浮點(diǎn)數(shù)默認(rèn)的類型為double類型
System.out.println(age+number+mon+nuk);//+會(huì)累加求和
System.out.println("相當(dāng)于字符串的連接"+age+number+mon+nuk);//+相當(dāng)于字符串的連接
}
}
/**
- Java中的運(yùn)算符
- 是一種特殊的符號(hào),用以表示數(shù)據(jù)的運(yùn)算、賦值和比較
- 運(yùn)算符的種類:
- 1.算術(shù)運(yùn)算符:+、 ﹣、 *、 /、 %(求余數(shù))、 ++(自增加1)、 --(自減1)
- 2.賦值運(yùn)算符: =、 +=、 ﹣=、 *=、 /=、 %=
- 3.比較運(yùn)算符:>、 <、 >=、 <=、 ==、 !=、 注意:比較運(yùn)算符的最終結(jié)果為boolean類型
- 4.邏輯運(yùn)算符
- @author ttc
*/
public class OperatorDemo {
public static void main(String[] args) {
int a = 5;
int b = 2;
int c = a + b;
int d = a / b;
int e = a % b;
String s1 = "aaa";
String s2 = "bbb";
// 關(guān)系運(yùn)算符--------------------------------start
boolean b1 = a>b;
boolean b2 = a<b;
System.out.println(b1);//true
System.out.println(b2);//false
System.out.println(a>=b);//true
System.out.println(a<=b);//false
System.out.println(a==b);//false
System.out.println(a!=b);//true
// 關(guān)系運(yùn)算符--------------------------------end
// 賦值運(yùn)算符--------------------------------start
// System.out.println(a+=b);
// System.out.println(a-=b);
// System.out.println(a*=b);
// System.out.println(a/=b);
// System.out.println(a%=b);
// 賦值運(yùn)算符--------------------------------end
// 算術(shù)運(yùn)算符--------------------------------start
System.out.println("---------------------------");
//算法:(a++)=5 a=6 (++a)=7 a=7 (a--)=7 a=6 (--a)=5 a=5
// int f = (a++)+(++a)+(a--)+(--a);
// System.out.println(f);//24
// System.out.println(s1+f+s2);
//int f = ++a;//a++:a先參與運(yùn)算,之后a自身加1
//System.out.println("f="+f);
//System.out.println("a="+a);
System.out.println("---------------------------");
System.out.println(c);
System.out.println(d);
System.out.println("5除以2的余數(shù)為:"+e);
// 算術(shù)運(yùn)算符--------------------------------end
}
}
/**
- 創(chuàng)建一個(gè)Person類(抽象的概念)
- 屬性:姓名(String) 性別(char) 年齡(int) 出生年月(String) 所在學(xué)校(String)
- 8種基本數(shù)據(jù)類型:byte short int long float double char boolean
- 整數(shù)類型默認(rèn)值:0 浮點(diǎn)類型默認(rèn)值:0.0 boolean的默認(rèn)值:false
- 創(chuàng)建變量的基本語(yǔ)法: 數(shù)據(jù)類型 變量名[=初始值];
- 類型轉(zhuǎn)換:
自動(dòng)類型轉(zhuǎn)換(由小到大)int a = 10;double d = a;強(qiáng)制類型轉(zhuǎn)換(由大到?。?double k = 3.14;int y = (int)k;
*規(guī)則:
*1.不同類型的變量進(jìn)行運(yùn)算最后得到的是位數(shù)最大的類型 int a = 1 float f = 3.14F double d = 12.90 double dd = a+f+d
*2.字符串與任何基本數(shù)據(jù)類型連接后,最終的結(jié)果都是字符串 System.out.println("hello"+a+d);
- @author zjjlive
*/
@SuppressWarnings("unused")
public class Person {
public static void main(String[] args) {
String name = "tom";
char sex = 'M';
int age = 30;
String birthday = "1995-6-13";
String school = "foreknow test";
boolean marry = true;
double salary = 2000.50;
float fff = '1';
System.out.println(fff);
System.out.println(name);
System.out.println(sex);
System.out.println(age);
System.out.println(birthday);
System.out.println(school);
System.out.println(marry);
System.out.println(salary);
}
}
/**
- Java中的運(yùn)算符
- 是一種特殊的符號(hào),用以表示數(shù)據(jù)的運(yùn)算、賦值和比較
- 運(yùn)算符的種類:
- 1.算術(shù)運(yùn)算符:+、 ﹣、 *、 /、 %(求余數(shù))、 ++(自增加1)、 --(自減1)
- 2.賦值運(yùn)算符: =、 +=、 ﹣=、 *=、 /=、 %=
- 3.比較運(yùn)算符:>、 <、 >=、 <=、 ==、 !=、 注意:比較運(yùn)算符的最終結(jié)果為boolean類型
- 4.邏輯運(yùn)算符: &&(邏輯與) ||(邏輯或) !(邏輯非)
- 運(yùn)算規(guī)則 : true && true=true true || true = true !isRight
true && false=false true || false = truefalse && true=false false || true = truefalse && false=false false || false = false- 在實(shí)際中如何使用: 20<x<100 等價(jià)于(x>20) && (x<100)
- @author ttc
*/
public class OperatorDemo {
public static void main(String[] args) {
int x = 140;
int a = 5;
int b = 2;
int c = a + b;
int d = a / b;
int e = a % b;
System.out.println(d);
String s1 = "aaa";
String s2 = "bbb";
System.out.println(x>20 && x<100);//20<x<100
System.out.println(x>20 || x<100);
boolean isRight = false;
System.out.println("取反:"+!isRight);
// 關(guān)系運(yùn)算符--------------------------------start
boolean b1 = a>b;
boolean b2 = a<b;
System.out.println(b1);//true
System.out.println(b2);//false
System.out.println(a>=b);//true
System.out.println(a<=b);//false
System.out.println(a==b);//false
System.out.println(a!=b);//true
// 關(guān)系運(yùn)算符--------------------------------end
// 賦值運(yùn)算符--------------------------------start
// System.out.println(a+=b);
// System.out.println(a-=b);
// System.out.println(a*=b);
// System.out.println(a/=b);
// System.out.println(a%=b);
// 賦值運(yùn)算符--------------------------------end
// 算術(shù)運(yùn)算符--------------------------------start
System.out.println("---------------------------");
//算法:(a++)=5 a=6 (++a)=7 a=7 (a--)=7 a=6 (--a)=5 a=5
// int f = (a++)+(++a)+(a--)+(--a);
// System.out.println(f);//24
// System.out.println(s1+f+s2);
//int f = ++a;//a++:a先參與運(yùn)算,之后a自身加1
//System.out.println("f="+f);
//System.out.println("a="+a);
System.out.println("---------------------------");
System.out.println(c);
System.out.println(d);
System.out.println("5除以2的余數(shù)為:"+e);
// 算術(shù)運(yùn)算符--------------------------------end
}
}
**
- if 條件判斷 (單一條件)
- 基本語(yǔ)法:
- if(表達(dá)式){
執(zhí)行語(yǔ)句;- }
- 規(guī)則:如果條件表達(dá)式為true 就會(huì)執(zhí)行化括號(hào)里面的語(yǔ)句 如果條件表達(dá)式為false會(huì)執(zhí)行if之后的語(yǔ)句
- if 條件判斷(二選一)
- 語(yǔ)法:
- if(表達(dá)式){
- 語(yǔ)句塊1;
- }else{
- 語(yǔ)句塊2;
- }
- @author ttc
*/
public class IFControlDemo {
public static void main(String[] args) {
int x = 160;
// if(x>=60){
// System.out.println("太好了,及格了?。。。?!");
// }
// System.out.println("我很愛學(xué)習(xí)java!!!!!!");
System.out.println("---------------------------------------------");
if(x>=60) {
System.out.println("及格了!?。。。?!");
}else {
System.out.println("不及格?。。。。?!");
}
System.out.println("需要重修?。。。。?!");
System.out.println("---------------------------------------------");
boolean b = true;
if (b=false) {
System.out.println("111111111111111");
} else {
System.out.println("222222222222222");
}
System.out.println(b);
System.out.println("-----------------------------------------------");
int y = 2;
if(y<2){
++y;
}
else{
y--;
}
System.out.println("y="+y);
System.out.println("-------------------------------------------------");
// y=y<2?++y:y--;
// System.out.println("y="+y);
// int z=(y--);
// System.out.println(z);
// System.out.println(y);
}
}
/**
- 數(shù)組:數(shù)組可以看成是多個(gè)相同類型數(shù)據(jù)的組合, 實(shí)現(xiàn)對(duì)這些數(shù)據(jù)的統(tǒng)一管理。也可以將數(shù)組看成是一個(gè)容器 元素:數(shù)組中的每一個(gè)數(shù)據(jù)都可以稱為一個(gè)元素
- 數(shù)組的長(zhǎng)度:數(shù)組中元素的個(gè)數(shù) 數(shù)組的分類:一維數(shù)組、多維數(shù)組 一維數(shù)組的定義: 數(shù)組類型[] 數(shù)組名; 如何對(duì)數(shù)組進(jìn)行初始化?數(shù)組類型[] 數(shù)組名 =
- new 數(shù)組類型[長(zhǎng)度]; 動(dòng)態(tài)初始化 1. int[] array = new int[5]; 靜態(tài)初始化 1. int[] array1 = new
- int[]{1,23,4,5,66,77,12}; 2. String[] s =
- {"java","c++","python","javascript","spring"}; 如何獲取到數(shù)組中某一個(gè)元素:數(shù)組名[下標(biāo)] 例如:s[1]
- = "c++" 注意:數(shù)組的下標(biāo)是從0開始的
- @author ttc
*/
public class ArrayDemo {
public static void main(String[] args) {
// 定義一個(gè)整型數(shù)組,數(shù)組名為array并進(jìn)行初始化
// int[] array = new int[5];
// //如何對(duì)數(shù)組進(jìn)行賦值
// array[0] = 12;
// array[1] = 1;
//// array[2] = 3;
//// array[3] = 14;
//// array[4] = 5;
//
// System.out.println(array[4]);
//
// String[] s1 = new String[4];
// s1[0] = "tom0";
// s1[1] = "tom1";
// s1[2] = "tom2";
// s1[3] = "tom3";
// System.out.println(s1[3]);
//
// //靜態(tài)初始化
// int[] array1 = new int[]{1,23,4,5,66,77,12};
// System.out.println(array1[1]);
// System.out.println("數(shù)組的長(zhǎng)度為:"+array1.length);
//
// String[] s = {"java","c++","python","javascript","spring"};
// System.out.println("輸出數(shù)組中的某一個(gè)元素:"+s[4]);
//
// System.out.println("------------------------------------------------");
// //如何將數(shù)組中的元素一次輸出(對(duì)數(shù)組進(jìn)行遍歷),可以使用循環(huán)
// for(int i = 0;i<s.length;i++) {
// System.out.println(s[i]);
// }
//
// //練習(xí):已知數(shù)組[2,14,68,15,3,66,5,35] 用戶從控制臺(tái)輸入一個(gè)數(shù)并判斷這個(gè)數(shù)在數(shù)組中是否存在
// //思路:
// //1.對(duì)數(shù)組進(jìn)行遍歷操作
// //2.每遍歷一次需要判斷用戶輸入的這個(gè)數(shù)與數(shù)組中的某一個(gè)元素是否相等,如果相等跳出循環(huán)
//
// Scanner input = new Scanner(System.in);
// boolean isRigth = false;
// System.out.println("請(qǐng)輸入一個(gè)數(shù):");
// int guess = input.nextInt();
// int[] arr = {2,14,68,15,3,66,5,35};
// for(int i = 0;i<arr.length;i++){
// if(guess==arr[i]) {
// isRigth = true;
// break;
// }
// }
// if(isRigth) {
// System.out.println("success......");
// }
//
// //已知數(shù)組[2,14,68,15,3,66,5,35] 求數(shù)組中所有數(shù)的奇數(shù)和以及偶數(shù)和
//
// //二維數(shù)組的定義以及初始化(數(shù)組的數(shù)組)
// int[][] arr1 = new int[3][4];
// arr1[0][0] = 12;
// arr1[0][1] = 13;
// arr1[0][2] = 14;
// arr1[0][3] = 15;
//
// arr1[1][0] = 11;
// arr1[1][1] = 12;
// arr1[1][2] = 13;
// arr1[1][3] = 14;
//
// arr1[2][0] = 22;
// arr1[2][1] = 23;
// arr1[2][2] = 24;
// arr1[2][3] = 25;
//
//
//
// int[][] arr2 = {{1,2,3},{4,5,6},{7,8,9}};
// System.out.println(arr2[0][1]);
//
// int[][] a = new int[3][ ];
// a[0] = new int[2];
// a[1] = new int[3];
// a[2] = new int[4];
// System.out.println(a[0][0]);
int[][] a = { { 1 }, { 4, 5, 6 }, { 7, 8 } };
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();
}
// [3,1,4,67,13,12,2]
}
}
/**
函數(shù):函數(shù)是完成某個(gè)功能的一組語(yǔ)句,通常將常用的功能寫成一個(gè)函數(shù) 例如:input.next()
函數(shù)有兩種:1.使用別人寫好的函數(shù),我們調(diào)用 2.自定義的函數(shù)
語(yǔ)法:
-
[訪問(wèn)控制符] [修飾符] 返回值類型 函數(shù)名(參數(shù)類型 形式參數(shù),參數(shù)類型 形式參數(shù),…)
{
函數(shù)體
}
返回值類型有兩種:1.有返回值的 2.無(wú)返回值 void
如何調(diào)用函數(shù):在主方中使用 類名.函數(shù)名(實(shí)參)
參數(shù)的類型:可以是基本類型(8種)/引用類型 String、數(shù)組
注意:自定義函數(shù)要與在類中,不能寫在main方法中,因?yàn)閖ava的函數(shù)是不能嵌套的
形式參數(shù):在方法被調(diào)用時(shí)用于接受外部傳入的變量(用戶的輸入)
? 參數(shù)類型:就是該形式參數(shù)的數(shù)據(jù)類型
返回值:方法在執(zhí)行完畢后返回給調(diào)用它的程序的數(shù)據(jù)(表示函數(shù)的最終的結(jié)果)
? 返回值類型:函數(shù)要返回的結(jié)果的數(shù)據(jù)類型(基本類型、引用類型)
注意:如果一個(gè)函數(shù)有返回值類型就一定要寫return(表示函數(shù)的最終結(jié)果)什么時(shí)候定義有返回值的方法,什么時(shí)候定義無(wú)返回值的方法 當(dāng)方法的調(diào)用者(main)需要利用函數(shù)返回值的結(jié)果參與其它運(yùn)算,就應(yīng)該定義為有返回值的方法,否則可以定義為無(wú)返回值的函數(shù) @author ttc
*/
public class FunctionDemo {
public static void eat(String tools){
System.out.println("我們可以使用"+tools+"來(lái)吃飯?。?!");
}
public static void sleep() {
System.out.println("sleep......");
}
/**
* 定義一個(gè)登錄的方法
* 思考:是否需要參數(shù)(username,password)
* @param args
*/
public static void login(String username,String password){
System.out.println("用戶名:"+username+"密碼:"+password);
}
public static void cacul() {
int[] array = {1,2,3,4,5,6,7};
for(int i = 0;i<array.length;i++){
System.out.println(array[i]);
}
}
/**
* 用戶從控制臺(tái)輸入兩個(gè)數(shù)并計(jì)算兩個(gè)數(shù)之和
* 用函數(shù)來(lái)實(shí)現(xiàn)
* @param args
*/
public static void sum(int a,int b) {
int sum = a+b;
System.out.println(sum);
}
/**
* 計(jì)算三個(gè)數(shù)之和
* @param a 輸入?yún)?shù)
* @param b 輸入?yún)?shù)
* @param c 輸入?yún)?shù)
* @return sum 和
*/
public static int total(int a,int b,int c) {
int sum = a+b+c;
return sum;
}
public static void main(String[] args) {
int sum = FunctionDemo.total(1, 2, 3);
int fol = sum+100;
System.out.println(sum);
System.out.println("-----------------------------------------");
//函數(shù)的調(diào)用
FunctionDemo.eat("筷子");
FunctionDemo.sleep();
FunctionDemo.login("tom","123456");
FunctionDemo.cacul();
Scanner intput = new Scanner(System.in);
System.out.println("請(qǐng)輸入第一個(gè)數(shù):");
int num1 = intput.nextInt();
System.out.println("請(qǐng)輸入第二個(gè)數(shù):");
int num2 = intput.nextInt();
//函數(shù)的調(diào)用
FunctionDemo.sum(num1, num2);
}
}
/**
- 函數(shù)是不能嵌套的但是可以互相調(diào)用
- 注意:main函數(shù)可以放到其它類中。main就相當(dāng)于測(cè)試函數(shù)
- @author ttc
*/
public class FunctionDemo2 {
public static int f1(int a, int b) {
return a + b;
}
public static String f2(String username, String password) {
int sum = f1(1, 2);
String school = f3("forknow");
return username + " " + password + sum + school;
}
public static String f3(String school) {
return school;
}
}
/**
- 冒泡排序(由小到大排序)
- 規(guī)則:相鄰的兩個(gè)元素進(jìn)行比較
- array:[3,1,16,22,11,4,5,7]
- [1,3,16,11,4,5,7,22]
- 思路:
- 需要使用嵌套for,外層的for循環(huán)控制總的比較次數(shù),內(nèi)層循環(huán)完成相鄰的兩個(gè)元素比較
- 相鄰的兩個(gè)元素比較?
- a = 2 b =1 如果a>b a = 1 b = 2 創(chuàng)建一個(gè)臨時(shí)變量(空瓶)
- @author ttc
*/
public class PopSort {
// public static void pop() {
// int[] array = {3,1,16,22,11,4,5,7};
// for(int i = 0;i<array.length;i++) {
// //相鄰的兩個(gè)元素比較
// for(int j = 0;j<array.length-1;j++) {
// if(array[j]>array[j+1]) {
// int temp = array[j];
// array[j] = array[j+1];
// array[j+1] = temp;
// }
// }
// }
// //對(duì)排序后的結(jié)果輸出
// for(int i = 0;i<array.length;i++) {
// System.out.println(array[i]);
// }
// }
// public static void pop(int[] array){
// for(int i = 0;i<array.length;i++) {
// //相鄰的兩個(gè)元素比較
// for(int j = 0;j<array.length-1;j++) {
// if(array[j]>array[j+1]) {
// int temp = array[j];
// array[j] = array[j+1];
// array[j+1] = temp;
// }
// }
// }
// //對(duì)排序后的結(jié)果輸出
// for(int i = 0;i<array.length;i++) {
// System.out.println(array[i]);
// }
// }
public static int[] pop(int[] array){
for(int i = 0;i<array.length;i++) {
//相鄰的兩個(gè)元素比較
for(int j = 0;j<array.length-1;j++) {
if(array[j]>array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
public static void main(String[] args) {
// PopSort.pop();
int[] array = {3,1,16,22,11,4,5,7};
int[] a = PopSort.pop(array);
//對(duì)排序后的結(jié)果輸出
for(int i = 0;i<a.length;i++) {
System.out.println(a[i]);
}
}
}
/**
- 構(gòu)造函數(shù)(構(gòu)造器):它是一個(gè)特殊的方法,這個(gè)方法沒有返回值類型,通過(guò)情況下訪問(wèn)修飾符都是public的
- 語(yǔ)法:
- public 類名(參數(shù)類型 參數(shù)名,參數(shù)類型 參數(shù)名....){
- }
- 構(gòu)造器的調(diào)用:new 構(gòu)造器();
- 構(gòu)造器的作用:1.可以初始變量 2.可以初始化對(duì)象
- 注意:
- 在Java中,每個(gè)類都至少要有一個(gè)構(gòu)造方法,如果程序員沒有在類里定義構(gòu)造方法,系統(tǒng)會(huì)自動(dòng)為這個(gè)類產(chǎn)生一個(gè)默認(rèn)的構(gòu)造方法
- @author ttc
*/
public class ConstructorDemo {
String name;
int age;
String school;
User user;
//初始化變量
public ConstructorDemo(String name,int age,String school){
//this表示當(dāng)前對(duì)象(ConstructorDemo)自身
this.name=name;
this.age = age;
this.school = school;
user = new User();
//user.setEmail("zjjlive@163.com");
}
// public ConstructorDemo() {
// System.out.println("這是一個(gè)默認(rèn)的構(gòu)造函數(shù)......");
// }
public void method() {
System.out.println("method......");
}
public static void main(String[] args) {
ConstructorDemo cDemo = new ConstructorDemo("tom",30,"foreknow");
}
}
/**
- 在一個(gè)構(gòu)造方法中,調(diào)用其它重載的構(gòu)造方法
- 語(yǔ)法:
- this();
- 規(guī)則: this();只能寫在構(gòu)造器的第一行。
- @author ttc
/
public class ConstructorDemo2 {
public ConstructorDemo2() {
System.out.println("D......................");
}
public ConstructorDemo2(int a) {
this();
System.out.println("A......................");
}
public ConstructorDemo2(int a,int b) {
this(a);
System.out.println("B......................");
}
public ConstructorDemo2(int a,int b,int c) {
this(a, b);
System.out.println("C......................");
}
public static void main(String[] args) {
ConstructorDemo2 cDemo2 = new ConstructorDemo2();
}
}
/*
- 方法的重載:方法名相同,參數(shù)以及類型不同,與返回值類型無(wú)關(guān)
- 變量的作用域:
- 1.出現(xiàn)在main方法中的變量,稱為局部變量(只在main中有效),一定要先初始化,然后再使用
例如:int a = 0; String name = null; boolean isRight = false; Student s = null; Scanner s = null;- 2.出現(xiàn)在方法中的變量,稱為局部變量(只在當(dāng)前方法中有效),一定要先初始化,然后再使用
public class A{public void method(int a,String n,User u){int max = 0;一定要先初始化,然后再使用}public static void main(String[] args){A a = new A();User user = new User();a.method(1,"abc",user);}}- 3.出現(xiàn)在類中的屬性(變量)為全局變量,作用域在整個(gè)類中都有效
- @author ttc
*/
public class OverLoadDemo {
public int sum(int a,int b) {
int c = a+b;
return c;
}
public void sum(int a,int b,int c) {
int d = a+b+c;
System.out.println(d);
}
public static void main(String[] args) {
OverLoadDemo oDemo = new OverLoadDemo();
int s = oDemo.sum(1, 2);
System.out.println(s);
oDemo.sum(3, 4, 5);
}
}
**
- 匿名對(duì)象
- 語(yǔ)法:
- new 類名().方法名()
- 什么時(shí)候使用:只需要調(diào)用當(dāng)前對(duì)象方法一次
- @author ttc
*/
public class SayHello {
public String say() {
return "say";
}
public void abc() {
System.out.println("abc......");
}
public String method() {
// SayHello sayHello =new SayHello();
// String ss = sayHello.say();
return new SayHello().say();
}
public static void main(String[] args) {
SayHello sHello = new SayHello();
sHello.say();
sHello.abc();
//匿名對(duì)象
new SayHello().say();
new SayHello().abc();
}
}
/**
- static關(guān)鍵字
? static可以修飾的元素
? 屬性:可以被多個(gè)對(duì)象共享 在實(shí)際中經(jīng)常被用于累加器 調(diào)用:類名.靜態(tài)變量 StaticDemo.a
? 方法:類名.方法名()
? 代碼塊
static{
//作用是負(fù)責(zé)初始化(變量、對(duì)象)
}
注意:靜態(tài)代碼塊無(wú)需調(diào)用(了解)
需要注意的問(wèn)題
? 只能修飾類成員,不能修飾局部變量。
靜態(tài)方法里只能直接訪問(wèn)靜態(tài)成員,而不能直接訪問(wèn)類中的非靜態(tài)成員
? 靜態(tài)方法中不能使用this關(guān)鍵字
? 靜態(tài)方法不能修飾構(gòu)造方法 - @author ttc
*/
public class StaticDemo {
String name;//成員變量(實(shí)例變量)
static int a;//靜態(tài)變量
public StaticDemo() {
a++;
}
//靜態(tài)方法
public static void method() {
System.out.println("static method......");
}
//實(shí)例方法(成員方法)
public void test() {
System.out.println("test.....");
}
public static void main(String[] args) {
//調(diào)用靜態(tài)方法
StaticDemo.method();
StaticDemo s1 = new StaticDemo();
System.out.println(s1.name);
System.out.println(StaticDemo.a);
StaticDemo s2 = new StaticDemo();
System.out.println(s2.name);
System.out.println(StaticDemo.a);
StaticDemo s3 = new StaticDemo();
System.out.println(s3.name);
System.out.println(StaticDemo.a);
StaticDemo s4 = new StaticDemo();
System.out.println(s4.name);
System.out.println(StaticDemo.a);
}
}
/**
- 面向?qū)ο缶幊蹋壕褪敲嫦蝾惥幊?,類是一個(gè)抽象的概念(是具有相同屬性和行為的一個(gè)模板),而對(duì)象是一個(gè)具體的。
- 特征:
- 1.封裝
? 2.繼承
? 3.多態(tài)
類中只能定義屬性(變量)與行為(方法)
public class 類名{
成員變量(屬性)
成員方法(函數(shù))
}
如何創(chuàng)建對(duì)象
類名 引用變量 = new 類名(); Scanner input = new Scanner(System.in)
如何訪問(wèn)當(dāng)前對(duì)象的屬性以及方法:
1.引用變量.屬性名 2. 引用變量.方法名() - @author ttc
*/
public class Student {
int sid;//學(xué)號(hào)
String name;//姓名
String school;//所在學(xué)校
int age;//年齡
public void eat() {
System.out.println("eat......");
}
public void sleep() {
System.out.println("sleep......");
}
public void study() {
System.out.println("study......");
}
public static void main(String[] args) {
//如何創(chuàng)建對(duì)象
Student s1 = new Student();
s1.sid = 1000;
s1.name = "tom";
s1.age = 20;
s1.school = "neusoft";
s1.eat();
s1.sleep();
s1.study();
System.out.println(s1.sid+"--"+s1.name+"--"+s1.age+"--"+s1.school);
Student s2 = new Student();
s2.sid = 1001;
s2.name = "jazz";
s2.age = 30;
s2.school = "neusoft";
s2.eat();
s2.sleep();
s2.study();
System.out.println(s2.sid+"--"+s2.name+"--"+s2.age+"--"+s2.school);
}
}
/**
- User類:可以稱為一個(gè)Javabean(就是一個(gè)普通的java類),只包含get/set方法
- 規(guī)則:所有的屬性都定義為private(私有的)
- private:只有當(dāng)前類可以使用,對(duì)于其它類是不可見的(封裝)
- 構(gòu)造器的重載:構(gòu)造器的名稱相同,但是參數(shù)不同(類型) 例如:println(String) println(char) println(int)
- @author ttc
*/
public class User {
private String email;
private String password;
private String confirm_pass;
private String require_code;
private String phone;
public User() {
}
public User(String email,String password,String confirm_pass) {
this.email = email;
}
//獲取email的屬性值
public String getEmail() {
return email;
}
//可以給User對(duì)象的屬性email進(jìn)行初始化(設(shè)置一個(gè)值)
public void setEmail(String email) {
this.email = email;
}
}