Java面向对象编程

时间:2021-02-26 13:26:41   收藏:0   阅读:0

面向对象编程

面向过程 & 面向对象

什么是面向对象

回顾方法及加深

//Demo01  类
public class Demo01 {

    //main  方法
    public static void main(String[] args) {

    }
    /*
    修饰符   返回值类型   方法名(...){
        //方法体
        return 返回值;
    }
     */
    //return 结束方法,返回一个结果!
    public String sayHello(){
        return "helle,world";
    }
    public void hello(){
        return;
    }
    public int max(int a,int b){
        return a>b ? a:b;
    }
}
public class Demo02 {
    //静态方法  加了static
    //非静态方法  没有加static
    public static void main(String[] args) {
        //非静态方法
        //实例化这个类 new
        //对象类型  对象名 = 对象值;
        Student student = new Student();
        student.say();
    }
    public static class Student{
        public void say(){
            System.out.println("学生说话了");
        }
    }
    //static和类一起加载的
    public static void a(){
        // b();
    }
    //类实例化之后才存在
    public void b(){

    }
}
public class Demo03 {
    public static void main(String[] args) {
        //实际参数和形式参数的类型要对应!
        int add = Demo03.add(1,2);//实际参数
        System.out.println(add);
    }

                        //形式参数
    public static int add(int a,int b){
        return a+b;
    }
}
public class Demo05 {
    //值传递
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);
        Demo05.change(a);
        System.out.println(a);
    }
    //返回值为空
    public static void change(int a){
        a = 10;
    }
}
public class Demo04 {
    //引用传递:对象,本质还是值传递
    //对象,内存!
    public static void main(String[] args) {
        Perosn perosn = new Perosn();
        System.out.println(perosn.name);//null
        Demo04.change(perosn);
        System.out.println(perosn.name);//秦疆
    }
    public static void change(Perosn perosn){
        //perosn是一个对象:指向的--->Perosn perosn = new Perosn();这是一个具体的人,可以改变属性
        perosn.name= "秦疆";
    }
    //定义一个Perosn类,有一个属性:name
    static class Perosn{
        String name;  //null
    }
}

java面向对象编程(类、对象)

JAVA的面向对象编程

javaOOP(面向对象编程)理念

类和对象

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!