一、運算順序
- 算數(shù)運算符:+ - * / % ++ --
- 邏輯運算符:&& || ! ^ & |
- 賦值運算符:= += -= *= /= //= %= **=
- 只有引用類型可以new(包括String);
- String string = String(original:"123");
- 域名倒置;
二、權(quán)限修飾符
| 作用域 | 當前類 | 同package | 子孫類 | 其他package |
|---|---|---|---|---|
| public | v | v | v | v |
| protected | v | v | v | x |
| friendly(default) | v | v | x | |
| private | v | x | x |
為什么用?
- 對自己而言,保護代碼不被污染;
- 對別人而言,給別人一個干凈的類;
三、構(gòu)造方法:
1.構(gòu)造方法:壓根不寫返回值;
2.重載的構(gòu)造方法:同一個類里,名字一樣,參數(shù)不一樣。
/**
*壓根不寫返回值
*/
public Dog(){
System.out.println("執(zhí)行了構(gòu)造方法");
}
3.特性:
(1.)new其實是在調(diào)用構(gòu)造方法;
(2.)如果一個類里邊沒有構(gòu)造方法,會自動創(chuàng)建一個空的構(gòu)造方法;
(3.)構(gòu)造方法能傳參數(shù),在構(gòu)造期間就把對象的值賦好;
(4.)一旦有了新的參數(shù)的構(gòu)造方法,空構(gòu)造就不在了,如果想保留,就手動寫上。
4.重載:
- *方法名相同;
- *方法的參數(shù)類型,參數(shù)個數(shù)不一樣;
- 方法的返回類型可以不一樣;
- 方法的修飾符可以不相同;
- main 方法也可以被重載。
四java的對象和類
Java作為一種面向?qū)ο笳Z言。支持以下基本概念:
多態(tài)
繼承
封裝
抽象
類
對象
實例
方法
重載
重點研究對象和類的概念:
- 對象:對象是類的一個實例(對象不是找個女朋友),有狀態(tài)和行為。例如,一條狗是一個對象,它的狀態(tài)有:顏色、名字、品種;行為有:搖尾巴、叫、吃等。
- 類:類是一個模板,它描述一類對象的行為和狀態(tài)。
package com.xinzhi.vehicle;
/**
* @author 荊少奇
* @date 2020/10/30
*/
public class Car {
//屬性
private int whell;
private String colour;
private int age;
//構(gòu)造方法
public Car(String colour,int whell,int age){
this.colour = colour;
this.whell = whell;
this.age = age;
}
//普通方法
public void outUp(){
System.out.println("這個汽車有" + whell + "個輪子,顏色是" + colour +",用了" + age + "年了!");
}
//還有一些方法
public void setColour(String colour){
this.colour = colour;
}
public String getColour(String colour){
return this.colour;
}
public void setAge(int age){
if(age > 5){
age = 4;
}
this.age = age;
}
public int getAge(int age){
return this.age;
}
public void setWhell(int whell){
this.whell = whell;
}
public int getWhell(int whell){
return this.whell;
}
}
package com.xinzhi.vehicle;
/**
* @author 荊少奇
* @date 2020/10/30
*/
public class Train {
//屬性
private String color;
private String plate;
private int carriage;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getPlate() {
return plate;
}
public void setPlate(String plate) {
this.plate = plate;
}
public int getCarriage() {
return carriage;
}
public void setCarriage(int carriage) {
this.carriage = carriage;
}
//構(gòu)造方法
public Train(){
}
//私有方法
private void private1(){
System.out.println("車牌是" + plate);
}
//普通方法
public void outUp(){
private1();
System.out.println("火車顏色是" + color + ",車牌是" + plate + "有" + carriage + "節(jié)車廂。");
}
}
private String color;
private int age;
private String post;
private String kind;
public String getColor() {
return color;
}
```java
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public Dog(){
}
public Dog(String color,String post,String kind,String age){
System.out.println("狗狗的顏色是"+color+",它今年已經(jīng)"+age+"歲了,它游泳的標準姿勢是"+post+",他的種類是"+kind);
}
private void teast(){
System.out.println("小狗夠被主人梳理了毛發(fā)");
}
public void meet(){
teast();
System.out.println("其他小狗狗看起來很羨慕它!");
}
}
package com.xinzhi.people;
public class boy {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boy(){
}
public boy(int age,String name){
this.age = age;
this.name = name;
}
private void dressed(){
System.out.println(this.name+"在家穿上了衣服");
}
public void run(){
dressed();
System.out.println("然后去公園跑步,他今年"+this.age+"歲了,跑的飛快!");
}
}
package com.xinzhi.people;
public class CowDung {
private String smell;
private String function;
private String color;
private int big;
public void setFunction(String function){
this.function = function;
}
private void flower(String color,int big){
System.out.println("一朵鮮花插在了"+color+"色的長度大概在"+big+"厘米的牛糞上");
}
public CowDung(){};
public CowDung(String color,int big){
flower(color,big);
}
public void CdFction(){
System.out.println("牛糞的功能就是"+function);
}
}
// ---------------------------------------------------------
// Main方法對類的調(diào)用
package com.xinzhi.test;
import com.xinzhi.people.CowDung;
import com.xinzhi.people.Girl;
public class test2 {
public static void main(String[] args) {
Girl girl = new Girl("13",13,"xs");
girl.setName("sdf");
System.out.println(girl.getName());
CowDung cd = new CowDung();
cd.setFunction("被鮮花插");
CowDung cowDung = new CowDung("黃",15);
cd.CdFction();
}
}