本文共 7482 字,大约阅读时间需要 24 分钟。
对于描述复杂的事物, 为了从宏观上把握, 从整体上合理分析, 我们需要使用面向对象的思路来分析整个系统. 但是, 具体到微观操作, 仍然需要面向过程的思路去处理
public class Demo{ //main方法 public static void main(String[] args){ } /* 修饰符, 返回值类型, 方法名(...){ //方法体 return 返回值; } */ public String sayHello(){ return "hello, world" } public int max(int a, int b){ return a > b? a: b; }}
public class Demo{ public static void main(String[] args){ //实际参数和形式参数类型要对应 int addNumber = Demo.add(1, 2); } public static int add(int a, int b){ return a+b; }}
public class Demo{ public static void main(String[] args){ int a = 1; Demo.change(a);//1 System.out.println(a);//1 } //返回值为空 pubic static void change(int a){ a = 10; }}
public class Demo{ public static void main(String[] args){ Person person = new Person(); System.out.println(person); //null Demo.change(person);//Flonx } public static void change(Person person){ //person 是一个对象, 指向的是一个具体的人, 可以改变属性 person.name = "Flonx"; }}//定义了一个Person类, 有一个属性: nameclass Person{ String name; //默认为null }
package com.Flonx.demo2;public class Program{ public static void main(String[] args){ //类: 抽象的, 需要实例化 Student xiaoMing = new Student(); Student xiaoHong = new Student(); System.out.println(xiaoMing.name);//null System.out.println(xiaoMing.age);//0 xiaoMing.name = "小明"; xiaoMing.age = 18; System.out.println(xiaoMing.name);//小明 System.out.println(xiaoMing.age);//18 }}
package com.Flonx.demo1; public class Student{ //属性: 字段 String name; int age; //方法 public void study(){ System.out.println(this.name+"在学习"); } }
和类名相同, 没有返回值
作用:
注意点:
package com.Flonx.demo1;public class Program{ public static void main(String[] args){ //实例化了一个对象 Person person = new Person("FLONX"); System.out.println(person.name); }}
package com.Flonx.demo2;public class Person{ //一个类即使什么也不写, 也会存在一个默认的构造器(方法) String name; //实例化初始值 public Person(){ this.name = "Flonx"; } //有参构造: 一旦定义了有参构造, ,默认构造器消失, 无参构造器就必须显式定义 public Person(String name){ this.name = name; }}
public class Application{ public static void main(String args){ Pet dog = new Pet(); dog.name = "旺财"; dog.age = 3; dog.shout(); System.out.println(); System.out.println(); Pet cat = new Pet(); }}class Pet{ String name; int age; public void shout(String name){ System.out.println(name+"在叫"); }}
package com.Flonx.Practice;import java.util.Scanner;public class Practice { public static void main(String[] args) { Person person = new Person(); person.test(); //person Student student = new Student(); student.test(); //student Person person1 = new Student(); person1.test(); //person-------------->方法的调用只和左边, 也就是定义的数据类型有关 Person person2 = new Person(); person2.test2(); //person Student student2 = new Student(); student2.test2(); //student Person person3 = new Student(); //子类重写了父类的方法 person3.test2(); //student---------------------->静态方法和非静态方法区别很大 }}class Person { public static void test() { System.out.println("test1--Person"); } protected void test2() { System.out.println("test2--person"); }}class Student extends Person { public static void test() { System.out.println("test1--Student"); } public void test2() { System.out.println("test2--student"); }}
Object object = new Student();//Object > String//Object > Person > Teacher//Object > Person > StudentSystem.out.println(object instance of Student);//trueSystem.out.println(object instance of Person);//trueSystem.out.println(object instance of Object);//trueSystem.out.println(object instance of Teacher);//falseSystem.out.println(object instance of String);//true
如果方法是静态的, 方法的调用与右边无关, 对象是什么类型就调用对应类的方法. 如果方法是实例的, (即满足重写条件), 且被子类重写后, 如果发生向上转型, 那么调用子类重写后的方法, 目的是如果有多个子类, 便于统一接口
强制转换:
package com.Flonx.Practice;import java.util.Scanner;public class Practice { public static void main(String[] args) { Person person = new Student(); Student student = (Student)person; student.test2(); student.test3(); Student student1 = new Student(); Person person1 = student1; person1.test2(); //person1.test4(); error }}class Person { public static void test() { System.out.println("test1--Person"); } public void test3(){ System.out.println("test3-Person"); } protected void test2() { System.out.println("test2--person"); }}class Student extends Person { public static void test() { System.out.println("test1--Student"); } public void test2() { System.out.println("test2--student"); } public void test4(){ System.out.println("test4-Student"); }}/*
转载地址:http://kvvzb.baihongyu.com/