Java入门3

打印 上一主题 下一主题

主题 1008|帖子 1008|积分 3024

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

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换行
  1. // pringln 字符串拼接输出
  2. System.out.println("My name is " + this.name + "!");
  3. // printf 格式化传参输出
  4. System.out.printf("My name is %s!",this.name);
复制代码
println 和 print 区别

println 可以自动换行,但是 print 不行,使用方法同 println 需要使用 + 进行字符串拼接
类(构造方法)

定义:对某些事物共性的抽取
  1. // 定义Student类
  2. // package com.iweb.demo02;
  3. public class Student {
  4.     // 成员变量
  5.     public int sno;
  6.     public String name;
  7.     public Float height;
  8.     public char gender;
  9.     public boolean inSingle;
  10.     // 重写构造方法
  11.     public Student(int sno, String name, Float height, char gender, boolean inSingle) {
  12.         this.sno = sno;
  13.         this.name = name;
  14.         this.height = height;
  15.         this.gender = gender;
  16.         this.inSingle = inSingle;
  17.     }
  18.     // 成员方法
  19.     public void eat(){
  20.         System.out.println("Student => eat()");
  21.     }
  22.     // 重载eat方法
  23.     public void eat(String tmp){
  24.         System.out.println("Student => sat(tmp)");
  25.     }
  26. }
复制代码
  1. // 主运行类
  2. // package com.iweb;
  3. import com.iweb.demo02.Student; // 引用类
  4. public class Application {
  5.     public static void main(String[] args) {
  6.         Student student = new Student(1001,"robot01",185f,'男',true); // 实例化对象
  7.         // 调用成员方法
  8.         student.eat();
  9.         student.eat("food01");
  10.     }
  11. }
复制代码
在类里定义引用型变量
  1. // 定义Person类
  2. // package com.iweb.demo02;
  3. public class Person {
  4.     public String name;
  5.     public String[] hobbies= {"123","123"};
  6.     public Dog dog;
  7.     public void display(){
  8.         System.out.print(this.name + " hobbies: ");
  9.         for(String hobby:this.hobbies){
  10.             System.out.print(hobby + " ");
  11.         }
  12.         System.out.println("\n My dog name is " + this.dog.name);
  13.     }
  14.     public void setDog(Dog dog) {
  15.         this.dog = dog;
  16.     }
  17.     public void setName(String name){
  18.         this.name = name;
  19.     }
  20. }
复制代码
  1. // 定义Dog类
  2. // package com.iweb.demo02;
  3. public class Dog {
  4.     public String name;
  5.     public void run(){
  6.         System.out.println("run() => Dog");
  7.     }
  8.     public void setName(String name) {
  9.         this.name = name;
  10.     }
  11. }
复制代码
  1. // 主运行类
  2. // package com.iweb;
  3. import com.iweb.demo02.Dog;
  4. import com.iweb.demo02.Person;
  5. public class Application {
  6.     public static void main(String[] args) {
  7.         Person person = new Person();
  8.         Dog dog = new Dog();
  9.         dog.setName("dog01");
  10.         person.setName("robot01");
  11.         person.setDog(dog);
  12.         person.display();
  13.     }
  14. }
复制代码
构造方法

无参构造
  1. // 定义Robots类
  2. // package com.iweb.demo02;
  3. public class Robots {
  4.     // 机器人姓名
  5.     public String name;
  6.     // 制造商
  7.     public String manufacturer;
  8.     // 产量
  9.     public int yield;
  10.     // 无参构造
  11.     public Robots() {
  12.         this.name = "robot01";
  13.     }
  14. }
复制代码
有参构造
  1. // 定义Robots类
  2. // package com.iweb.demo02;
  3. public class Robots {
  4.     // 机器人姓名
  5.     public String name;
  6.     // 制造商
  7.     public String manufacturer;
  8.     // 产量
  9.     public int yield;
  10.     // 有参构造
  11.     public Robots(String name, String manufacturer, int yield) {
  12.         this.name = name;
  13.         this.manufacturer = manufacturer;
  14.         this.yield = yield;
  15.     }
  16. }
复制代码
重载构造方法

让开发者选择不同的构造方法实例化对象
  1. // 定义Robots类
  2. // package com.iweb.demo02;
  3. public class Robots {
  4.     // 机器人姓名
  5.     public String name;
  6.     // 制造商
  7.     public String manufacturer;
  8.     // 产量
  9.     public int yield;
  10.     // 无参构造
  11.     public Robots() {
  12.         this.name = "robot01";
  13.     }
  14.     // 有参构造
  15.     public Robots(String name, String manufacturer, int yield) {
  16.         this.name = name;
  17.         this.manufacturer = manufacturer;
  18.         this.yield = yield;
  19.     }
  20. }
复制代码
  1. // 主运行类
  2. // package com.iweb;
  3. import com.iweb.demo02.Robots;
  4. public class Application {
  5.     public static void main(String[] args) {
  6.         // 调用无参构造
  7.         Robots robots = new Robots();
  8.         // 调用有参构造
  9.         Robots robots1 = new Robots("robot01","factory01",10);
  10.     }
  11. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

兜兜零元

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表