马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
面向对象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);
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |