面向物件1
面向物件,更在乎的結果,而程序的實作并不重要
IDea快捷鍵(基礎版)
| 快捷鍵 | 作用 |
|---|---|
| ctrl + / | 快捷注釋 |
| ctrl + shift + / | 多行注釋 |
| ctrl + d | 快速復制 |
| ctrl + shift + up/down | 移動代碼行數 |
| ctrl + z | 撤回 |
| ctrl + o | 快速重寫父類函式 |
| ctrl + alt + l | 自動格式化代碼 |
?printf 格式化傳參(輸出)
| 引數 | 對應資料型別 |
|---|---|
| %d | 整型 |
| %f | 浮點型 |
| %s | 字串 |
| %b | 布爾型 |
| %c | 字符型 |
| %n | 換行 |
// pringln 字串拼接輸出
System.out.println("My name is " + this.name + "!");
// printf 格式化傳參輸出
System.out.printf("My name is %s!",this.name);
println 和 print 區別
println 可以自動換行,但是 print 不行,使用方法同 println 需要使用 + 進行字串拼接
類(構造方法)
定義:對某些事物共性的抽取
// 定義Student類
// package com.iweb.demo02;
public class Student {
// 成員變數
public int sno;
public String name;
public Float height;
public char gender;
public boolean inSingle;
// 重寫構造方法
public Student(int sno, String name, Float height, char gender, boolean inSingle) {
this.sno = sno;
this.name = name;
this.height = height;
this.gender = gender;
this.inSingle = inSingle;
}
// 成員方法
public void eat(){
System.out.println("Student => eat()");
}
// 多載eat方法
public void eat(String tmp){
System.out.println("Student => sat(tmp)");
}
}
// 主運行類
// package com.iweb;
import com.iweb.demo02.Student; // 參考類
public class Application {
public static void main(String[] args) {
Student student = new Student(1001,"robot01",185f,'男',true); // 實體化物件
// 呼叫成員方法
student.eat();
student.eat("food01");
}
}
在類里定義參考型變數
// 定義Person類
// package com.iweb.demo02;
public class Person {
public String name;
public String[] hobbies= {"123","123"};
public Dog dog;
public void display(){
System.out.print(this.name + " hobbies: ");
for(String hobby:this.hobbies){
System.out.print(hobby + " ");
}
System.out.println("\n My dog name is " + this.dog.name);
}
public void setDog(Dog dog) {
this.dog = dog;
}
public void setName(String name){
this.name = name;
}
}
// 定義Dog類
// package com.iweb.demo02;
public class Dog {
public String name;
public void run(){
System.out.println("run() => Dog");
}
public void setName(String name) {
this.name = name;
}
}
// 主運行類
// package com.iweb;
import com.iweb.demo02.Dog;
import com.iweb.demo02.Person;
public class Application {
public static void main(String[] args) {
Person person = new Person();
Dog dog = new Dog();
dog.setName("dog01");
person.setName("robot01");
person.setDog(dog);
person.display();
}
}
構造方法
無參構造
// 定義Robots類
// package com.iweb.demo02;
public class Robots {
// 機器人姓名
public String name;
// 制造商
public String manufacturer;
// 產量
public int yield;
// 無參構造
public Robots() {
this.name = "robot01";
}
}
有參構造
// 定義Robots類
// package com.iweb.demo02;
public class Robots {
// 機器人姓名
public String name;
// 制造商
public String manufacturer;
// 產量
public int yield;
// 有參構造
public Robots(String name, String manufacturer, int yield) {
this.name = name;
this.manufacturer = manufacturer;
this.yield = yield;
}
}
多載構造方法
讓開發者選擇不同的構造方法實體化物件
// 定義Robots類
// package com.iweb.demo02;
public class Robots {
// 機器人姓名
public String name;
// 制造商
public String manufacturer;
// 產量
public int yield;
// 無參構造
public Robots() {
this.name = "robot01";
}
// 有參構造
public Robots(String name, String manufacturer, int yield) {
this.name = name;
this.manufacturer = manufacturer;
this.yield = yield;
}
}
// 主運行類
// package com.iweb;
import com.iweb.demo02.Robots;
public class Application {
public static void main(String[] args) {
// 呼叫無參構造
Robots robots = new Robots();
// 呼叫有參構造
Robots robots1 = new Robots("robot01","factory01",10);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/548580.html
標籤:其他
上一篇:Python面向物件-高級用法
