ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

2021.9.11 this关键字、 java封装、继承、方法重写

2021-09-11 18:05:47  阅读:181  来源: 互联网

标签:11 column java 2021.9 maxValue int location public row


1、this -----this调用成员属性

1. 定义
代表本类对象的引⽤
⽤来区分重名的成员变量和局部变量
2. 注意事项
只能在本类⽅法中使⽤
本类的静态⽅法中⽆法使⽤
当我们创建对象并调⽤对象的⽅法时, ⽅法中如果出现了this,指的就是当前创建的这个对象
本类对象中的⽅法调⽤本类中的其他⽅法时, 其实被调⽤⽅法的前⾯就有⼀个省略的this

static class Rectangle{
        int  width;
        int  height;
        Rectangle(){
        }
        public int getWidth() {
            return width;
        }
        public int getHeight() {
            return height;
        }
        public void setWidth(int width) {
            this.width = width;//this用来特指,当前的第一个width是类中的成员属性,而第二个width是方法中的参数
        }
        public void setHeight(int height) {
            this.height = height;//this用来特指,当前的第一个height是类中的成员属性,而第二个height是方法中的参数
	        }
}

2、题目

/*
定义一个类 Location, 用于定位二维数组中的最大值和他的下标。 这个类中存在三个属性,分别是row、column、maxValue。分别表示最大值的行-所在的元素数组的下标,最大值的列-在元素数组中的下标、以及最大值本身的数值。
在LocationDemo中定义方法 public static Location locationLargest(int[][] nums),返回的是location对象。
在main方法中调用这个方法,获取到location对象之后,调用他的showInfo的方法,打印出最大值的位置和他的数值
 */
  public  class Location{
    private int   row;
    private int column;
    private float maxValue;

    public Location() {
    }

    public Location(int row, int column, float maxValue) {
        this.row = row;
        this.column = column;
        this.maxValue = maxValue;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public float getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(float maxValue) {
        this.maxValue = maxValue;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Location location = (Location) o;
        return row == location.row &&
                column == location.column &&
                Float.compare(location.maxValue, maxValue) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(row, column, maxValue);
    }
    @Override
    public String toString() {
        return "Location{" +
                "row=" + row +
                ", column=" + column +
                ", maxValue=" + maxValue +
                '}';
    }
}
public class LocationDemo {
public static Location locationLargest(int[][] nums) {
    //获得最大值的行-所在的元素数组的下标,最大值的列-在元素数组中的下标、以及最大值本身的数值。
    int maxValue = nums[0][0];
    int row = 0;
    int column = 0;
    for (int i = 0; i < nums.length; i++) {
        for (int j = 0; j < nums[i].length; j++) {
            if (maxValue < nums[i][j]) {
                maxValue = nums[i][j];
                row = i;
                column = j;
            }
        }
    }
    Location location = new Location(row, column, maxValue);//用上面获得的三个数据创建location对象
    return location;//返回location对象
}
public static void  main(String[] args) {
    int[][] arr = {
            {1, 4, 6, 8, 4, 45, 76788, 23445, 878, 13346},
            {35, 6547, 25, 8, 23, 1, 98, 4, 2347, 23},
            {43, 7658, 234468, 23, 87, 23, 678, 3, 2, 345, 97}
    };
    Location location = locationLargest(arr);
    System.out.println(location);
    }
}

3、数组知识
数组的遍历
数组的最值
多维数组
数组的下标

4、java封装
将属性和对属性的操作封装在一起,以类的形式出现

  1. ⽣活中的封装
信息隐藏,隐藏对象的实现细节,不让⽤户看到
将东⻄都包装在⼀起,以新的完整的形式呈现出来
2. Java当中的封装
隐藏类的实现细节
让使⽤者只能通过实现订制好的⽅法来访问数据,可以⽅便加⼊控制逻辑,限制对于属性的不
合理操作
将⽅法和属性⼀起包装到⼀个单元中,单元以类的形式实现
便于修改,增强代码的可维护性
可进⾏数据的检查
3. 隐藏属性、⽅法或者实现细节的过程就是封装

5、访问权限修饰符
在这里插入图片描述

6、继承-----构造方法无法被继承

1. 定义
		从⽣活中的理解, 我们可以明⽩, 继承就是从前辈⼿⾥获取⼀些好处
		编程语⾔中的继承是指让类和类之间产⽣关系 , ⼦⽗类关系
 2.优缺点
	   优点
	就是为了获取到⽗类中的⽅法和属性
	提⾼了代码的复⽤性和维护性
	让类和类之间产⽣关系,是多态的前提
		缺点
	类的耦合性增强了 , 我们开发的原则是⾼内聚,低耦合
	耦合: 类与类的关系
	内聚: 独⽴完成⼯作的能⼒

7、super和this

public class test {
    public static void main(String[] args) {
        dog dog=new dog("动物","金毛");
        dog.sleep();
    }
}
class animal{
    String name;
    public animal(String name) {
        this.name = name;
        System.out.println("这是animal的构造方法");
    }
    void eat(){
        System.out.println("吃饭");
    }
}
class dog extends animal{
    String name;
    public dog(String name, String name1) {
        super(name);
        this.name = name1;
        System.out.println("这是dog的构造方法");
    }
    void sleep(){
        System.out.println("睡觉");
    }
}

在这里插入图片描述

8、方法重写

1. 定义
⼦⽗类中出现了⼀模⼀样的⽅法
重写是⼀种现象, 也是⼀个动作
当⼦类需要⽗类的功能, ⽽功能主体⼦类有⾃⼰特有内容时, 可以重写⽗类的⽅法, 这样 , 既延续了⽗类的功能 , ⼜定义了⼦类特有的内容
2、子类对象调用方法时,只调用子类自己的方法,不能调用父类的方法,子类方法覆盖了子类
3、如果非要调用父类的方法,可以在子类内部用super.方法名称 去调用父类方法
4、子类重写后的方法,只有类内部的具体实现和父类不同,其他都相同
5、用this.方法名称调用方法时,只调用子类方法,当子类中没有该方法时,才会调用父类中的该方法

标签:11,column,java,2021.9,maxValue,int,location,public,row
来源: https://blog.csdn.net/tpszhao/article/details/120233418

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有