类是对某一类事物共性的抽象概念,而对象描述的是一个具体的产物。
每个属性的集合就构成了一个对象,可是全部的属性都应该是群体的定义,而群体的定义就造成了一个类。
类是一个模板,而对象才是类可使用的实例。
成员属性:(Field)有时候会简化成其为属性好比——年龄,姓名,性别
操做方法:(Method)定义对象具备的处理行为好比——唱歌、跳舞、游泳、运动java
A:咱们学习编程是为了什么编程
B:咱们如何描述现实世界事物数组
C:Java中最基本的单位是类,Java中用class描述事物也是如此安全
D:定义类其实就是定义类的成员(成员变量和成员方法)学习
A:文件名问题测试
B:如何使用对象?this
C:如何使用成员变量呢?spa
D:如何使用成员方法呢?设计
* 属性:姓名,年龄,性别 * 行为:学习,睡觉 class JavaObject { public static void main(String [] args) { //类名 对象名 = new 类名(); Student s = new Student(); //调用属性并赋值 s.name = "张三"; s.age = 23; s.gender = "男"; System.out.println(s.name + "..." + s.age + "..." + s.gender); //使用变量 //调用方法 s.study(); s.sleep(); } } class Student { String name; //姓名 int age; //年龄属性 String gender; //性别属性 public void study() { //定义学习的方法; System.out.println("学生学习"); } public void sleep() { System.out.println("学生睡觉"); } }
* 车 * 成员变量:颜色,轮胎数 * 成员方法:跑 class JavaObject { public static void main(String [] args) { Car c = new Car(); //建立对象 c.color = "red";//调用属性并赋值 c.num = 4; c.run();//调用方法 Car c2 = new Car(); c2.color = "black"; c2.num = 3 ; c2.run(); c2 = null; //当引用设置为null,空指针异常。 c2.run(); Car c3 = c2; c2.run(); } } class Car { String color; //颜色 int num; //轮胎数 public void run() { System.out.println("color" + num); } }
A:在类中的位置不一样指针
B:在内存中的位置不一样
C:生命周期不一样
D:初始化值不一样
注意事项:
class JavaObject { public static void main(String [] args) { Person p = new Person(); p.speak(); } } class Person { String name; //成员变量 int num; public void speak() { //x和num都是局部变量 int num = 10; System.out.println(num); System.out.println(name); //就近原则 } }
A:方法的参数是类名public void print(Student s){}//print(new Student());
class JavaObject { public static void main(String [] args) { print(10); //赋值 Student s = new Student(); //建立对象 print(s); } public static void print(int x) { //形式参数是基本数据类型,传递的值 System.out.println(x); } //形式参数是引用数据类型,传递的地址值 public static void print(Student stu) { //Student stu = new Student(); stu.name = "张三"; stu.age = 23; stu.speak(); } } class Student { String name; //成员变量 int age; public void speak() { //局部变量 System.out.println(name + "..." + age); } }
a:调用方法
class JavaObject { public static void main(String [] args) { //Car c1 = new Car(); //建立有名字的对象 //c1.run(); //new Car().run(); //匿名对象调用完就变成垃圾了 //new Car().run(); //匿名对象只适合对方法的一次调用,由于调用屡次就会产生多个对象,不如用有名字对象,不然浪费内存 //匿名对象能够调用属性,可是没有意义,由于调用后就变成垃圾,若是须要赋值仍是用有名字对象 new Car().color = "red"; new Car().num = 23; new Car().run(); } } class Car { String color; int num; public void run() { System.out.println(color + "..." + num); } }
b:匿名对象能够做为实际参数传递
class JavaObject { public static void main(String [] args) { /*Car c1 = new Car(); //建立有名字的对象 c1.color = "red"; c1.num = 8; c1.run();*/ //Car c1 = new Car(); //method(c1); //method(new Car()); //Car c2 = new Car(); //method(c2); //method(new Car()); for (int i = 0;i < 10 ;i ++ ) { method(new Car()); } } //抽取方法,提升代码的复用性 protected default public static void method(Car cc) { //Car cc = new Car(); cc.color = "red"; cc.num = 8; cc.run(); } }
A:封装概述
B:封装好处
C:封装原则
B:private关键字特色
C:案例演示
public class JavaObject { public static void main(String [] args) { System.out.println(""); Person p1 = new Person(); p1.name = "张三"; //p1.age = 23; //p1.speak(); p1.setAge(-17); //int age1 = p1.getAge(); System.out.println(p1.getAge()); } } class Person { String name; private int age; //私有成员变量 public void setAge(int a) { //a = 17 //对外提供公共的访问方法 if (a > 18 && a<120) { age = a; } else { System.out.println("输入有误"); } } public int getAge() { //对外提供公共的访问方法 return age; } public void speak() { System.out.println(name + "..." + age); } }
A:this关键字特色
B:案例演示
public class JavaObject { public static void main(String [] args) { System.out.println(""); Person p1 = new Person(); p1.setName("张三"); p1.setAge(23); System.out.println(p1.getName() + "..." + p1.getAge()); Person p2 = new Person(); p2.setName("李四"); p2.setAge(32); p2.speak(); } } class Person { private String name; private int age; public void setName(String name) { this.name = name;//在这里this表明p1的地址值 } public String getName() { return name; } public void setAge(int age) { //a = 17 if (age > 18 && age<120) { this.age = age; } else { System.out.println("输入有误"); } } public int getAge() { return age; } public void speak() { System.out.println(name + "..." + age); } }
手机类
public class JavaObject { //建立对象phone public static void main(String [] args) { Phone p1 = new Phone(); //01,主方法声明p1,堆内存建立实体 p1.setBrand("Apple"); //03进栈内存 赋值brand p1.setPrice(5288); //05price System.out.println(p1.getBrand() + "..." + p1.getPrice());//09 } } class Phone { private String brand; //品牌 02null private int price; //价格 0 public void setBrand(String brand) { //设置品牌04赋值 this.brand = brand; } public String getBrand() { //获取品牌07 return brand; } public void setPrice(int price) { //设置价格 06 this.price = price; } public int getPrice() { //获取价格08 return price; } public void call() { System.out.println("打电话"); } public void sendMessage() { System.out.println("发信息"); } public void palyGame() { System.out.println("打游戏"); } }