面向对象(一)

面向对象———>描述现实生活

类,对象,成员,初始化,重载


  1. 把具有相同属性和相似行为的一类事物成为类。
    找出相似的行为和相同的属性的过程称为抽象。
    a.找出相同的属性称为数据抽象 ———>用数据来表示即可—>用数据类型
    b.找出相似的行为称为过程抽象 ———>用方法(函数)表示即可
    案例:
    人类:
    相同的属性——>身高,体重,年龄
    相似的行为——>吃饭,走路,睡觉等。
    通过封装行为构成类
    class 类名 {属性,行为}————>这个过程称为封装
    属性和行为谁先谁后是没有关系的,每个属性和行为都是并列的,不分先后。

  2. 对象
    类的实例就是对象(创建一个类的实例,创建一个对象)
    如何创建对象
    类名 对象(引用名)名 = new 类名();

  3. 成员
    成员变量:类的属性
    成员函数(方法):类的行为
    暂时而言:类中只能存放成员
    暂时而言:成员必须通过对象去访问 用运算符.
  4. 初始化(给成员函数进行赋值)
    a.成员变量有默认值 (0和null)
    b.对象创建成功之后就有初始值——>构造函数
    b1一个成员函数,该函数函数名称与类名相同
    b2无任何返回值类型,连void都没有
    b3创建对象的时候会自动调用,并且创建一个对象就会调用一次
    b4就现阶段而言,构造函数的主要作用是给成员赋初始值的。
    b5如果一个没有构造函数,那么JVM会给类自动添加一个无参数的构造函数,称之为默认的构造函数。如下:
    public 类名(){}//一旦。人为的给了类构造函数就不存在了
  5. 函数重载
    1.同一个类中的一组函数
    2.这组函数,函数名称都相同
    3.参数不同(类型不同,个数不同,类型个数都不同)
    4.在这个调用时,会根据参数的类型和个数自动匹配决定调用哪个,但是这种匹配未必是精确的,符合就近原则(找最接近的去匹配),如果没有最精确的,就找能够匹配上的,这种能够匹配得上的,不需要人为去转换。
    5.与函数的返回值类型无关
  6. 定义初始化
    1.给成员变量直接赋值就是定义初始化,当该类的所有对象的某个属性都一样的时候
    2顺序:也就是创建对象的时候构造函数,定义初始化都被执行
    a.定义初始化 {}
    b.如果存在定义初始化,定义初始化先执行,然后再执行构造函数。

封装案例

//Java开发实战经典 类设计分析
class  Student
{
        private String stuno;
        private String name;
        private float math;
        private float english;
        private float computer;
        public Student(){}
        public Student(String stuno,String name, float math,float english,float computer){
        /*  this.stuno=stuno;
            this.name=name;
            this.math=math;
            this.english=english;
            this.computer=computer;*/
            this.setStuno(stuno);
            this.setName(name);
            this.setMath(math);
            this.setEnglish(english);
            this.setComputer(computer);
        }
        public String getStuno(){
            return stuno;
        }
        public String setStuno(String stuno){
            return this.stuno=stuno;
        }
        public String getName(){
                return name;
        }
        public String  setName(String name){
            return this.name= name;
        }
        public float getMath(){
            return math;
        }
        public float setMath(float math){
            return this.math=math;
        }
        public float getEnglish(){
            return english;
        }
        public float setEnglish(float english){
            return this.english=english;
        }
        public float getComputer(){
            return computer;
        }
        public float  setComputer(float computer){
            return this.computer = computer;
        }
        public float sum(){
            return math+english+computer;
        }
        public float avg(){
            return this.sum()/3;
        }
        public float max(){
            float max=math;
            max = max>computer?max:computer;
            max = max >english?max:english;
            return max;
        }
        public float min(){
            float min = math;
            min = min < computer ?min:computer;
            min = min <english?min:english;
            return min;
        }
}

public class  ExampleDemo01{
        public static void main(String[] args){
            Student stu = new Student("MLDN-33","xx",95.0f , 89.0f ,96.0f);
            System.out.println("学生编号:"+stu.getStuno());
            System.out.println("学生姓名:"+stu.getName());
            System.out.println("数学成绩:"+stu.getMath());
            System.out.println("英语成绩:"+stu.getEnglish());
            System.out.println("计算机成绩:"+stu.getComputer());
            System.out.println("最高分:"+stu.max());
            System.out.println("最低分:"+stu.min());
        }
}

Java中常用的内存区域
在Java中主要存在4块内存空间,这些空间的名称及作用如下
(1)栈内存空间:保存所有的对象名称(更精确的说是保存了引用的堆内存的地址)
(2)堆内存空间:保存每个对象的具体属性内容
(3)全局数据区:保存static类型的数据
(4)全局代码区:保存所有的方法定义

this关键字

this引用(当前对象的引用)
1. this在什么地方
一个成员函数(非静态的)访问另外一个成员(非静态的)
前面都省略了this
2. 什么是this
当前对象(的引用)——>调用这个函数的那个对象(的引用)

class Person
{
    private String name;
    private int age;
    public Person(){
        System.out.println("一个新的Person对象被实例化。")
    }
    public Person(String name,int age){
        this.();
        this.name = name;
        this.age = age;
    }
    public String getInfo(){
        return "姓名:"+name+",年龄:"+age;
    }
}



class  ThisDemo03
{
    public static void main(String[] args) 
    {
        Person per1 = new Person("张三",33);
        System.out.println("Hello World!");
    }
}

this()只能放在构造方法首行
this调用构造方法时一定要留一个构造方法作为出口,即程序中只是存在一个构造方法不使用this调用其他构造方法。

static 关键字

如果在程序中使用static声明属性,则此属性称为全局属性(有些也称为静态属性)

class Person
{
    String name;
    int age;
    String country = "A城";
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    public void info(){
        System.out.println("姓名:"+this.name+",年龄:"+this.age+",城市:"+country);
    }
}
class  StaticDemo01
{
    public static void main(String[] args) 
    {
        Person p1 = new Person("张三",30);
        Person p2 = new Person("王五",31);
        Person p3 = new Person("李四",32);
        p1.info();
        p2.info();
        p3.info();
    }
}

用static声明的变量,成为该类的全局变量,当一个属性,比如这些人都是A城的时候就可以用static声明。

static 既可以在声明属性是使用,也可以用来声明方法,用它声明的方法有时也被称为“类方法”,可以由类名称直接调用。
说明使用static声明的属性是所有对象共享的。

/*使用static 声明方法
*/
class Person{
    private String name;
    private int age;
    private static String country ="A城";
    public static void setCountry(String c){
    country=c;
    }
    public Person(String name,int age){
        this.name = name;
        this.age=age;
    }
    public void info(){
        System.out.println("姓名:"+this.name+",年龄"+this.age+",城市"+country);
    }
    public static String getCountry(){
        return country;
    }
}
class StaticDemo04{
    public static void main(String[] args) 
    {
        Person p1 = new Person("张三",30);
        Person p2 = new Person("李四",31);
        Person p3 = new Person("王五",32);
        System.out.println("修改前之前========");
        p1.info();
        p2.info();
        p3.info();
        System.out.println("修改前之后========");
        Person.setCountry("B城");
        p1.info();
        p2.info();
        p3.info();
    }
}

使用satic声明方法,可以直接使用类名称调用。
在此处需要说明的是,非static声明的方法可以去调用static声明的属性或方法。但是static声明的方法是不能调用非static类型声明的属性或方法的。
主函数本身是静态的,只能访问静态成员。(如果一个方法需要由主方法直接调用,那么这个方法必须加上static关键字)
static为对象进行自动的编名操作

/*
范例:为对象自动进行编名
*/
class Demo
{
    private String name;
    private static int count=0;
    public Demo(){
        count++;
        this.name = "DEMO-"+count;
    }
    public Demo(String name){
        this.name= name;
    }
    public String getName(){
        return this.name;
    }
}
public class  StaticDemo07
{
    public static void main(String[] args) 
    {
        System.out.println(new Demo().getName());
        System.out.println(new Demo("LXH").getName());
        System.out.println(new Demo().getName());
        System.out.println(new Demo("MLDN").getName());
        System.out.println(new Demo().getName());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值