一、运算顺序
- 算数运算符:+ - * / % ++ --
- 逻辑运算符:&& || ! ^ & |
- 赋值运算符:= += -= *= /= //= %= **=
- 只有引用类型可以new(包括String);
- String string = String(original:"123");
- 域名倒置;
二、权限修饰符
作用域 | 当前类 | 同package | 子孙类 | 其他package |
---|---|---|---|---|
public | v | v | v | v |
protected | v | v | v | x |
friendly(default) | v | v | x | |
private | v | x | x |
为什么用?
- 对自己而言,保护代码不被污染;
- 对别人而言,给别人一个干净的类;
三、构造方法:
1.构造方法:压根不写返回值;
2.重载的构造方法:同一个类里,名字一样,参数不一样。
/**
*压根不写返回值
*/
public Dog(){
System.out.println("执行了构造方法");
}
3.特性:
(1.)new其实是在调用构造方法;
(2.)如果一个类里边没有构造方法,会自动创建一个空的构造方法;
(3.)构造方法能传参数,在构造期间就把对象的值赋好;
(4.)一旦有了新的参数的构造方法,空构造就不在了,如果想保留,就手动写上。
4.重载:
- *方法名相同;
- *方法的参数类型,参数个数不一样;
- 方法的返回类型可以不一样;
- 方法的修饰符可以不相同;
- main 方法也可以被重载。
四java的对象和类
Java作为一种面向对象语言。支持以下基本概念:
多态
继承
封装
抽象
类
对象
实例
方法
重载
重点研究对象和类的概念:
- 对象:对象是类的一个实例(对象不是找个女朋友),有状态和行为。例如,一条狗是一个对象,它的状态有:颜色、名字、品种;行为有:摇尾巴、叫、吃等。
- 类:类是一个模板,它描述一类对象的行为和状态。
package com.xinzhi.vehicle;
/**
* @author 荆少奇
* @date 2020/10/30
*/
public class Car {
//属性
private int whell;
private String colour;
private int age;
//构造方法
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;
}
//构造方法
public Train(){
}
//私有方法
private void private1(){
System.out.println("车牌是" + plate);
}
//普通方法
public void outUp(){
private1();
System.out.println("火车颜色是" + color + ",车牌是" + plate + "有" + carriage + "节车厢。");
}
}
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+",它今年已经"+age+"岁了,它游泳的标准姿势是"+post+",他的种类是"+kind);
}
private void teast(){
System.out.println("小狗够被主人梳理了毛发");
}
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方法对类的调用
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();
}
}