封装 (一)权限修饰符构造方法和重载方法以及练习

这篇博客介绍了Java编程的基础知识,包括数组操作、面向对象的封装概念、以及类和对象的使用。通过示例展示了如何创建具有特定条件的求和、数组倒置以及车的类。此外,还涉及到了方法的重载、构造器、this关键字的运用,并提供了多个类的实例,如Dog、Boy和Car,展示类的属性和方法。最后,通过Main方法调用这些类进行实际操作。

10.30日总结

一、前情回顾

class test1{
    public static void main(String args[]){
        //求和 个位不能为7 十位不能为7 必须是偶数
        int[] array = {1,3,4,5,6,7,8,9,17,177};
        int sum = 0;
        for (int i = 0;i < array.length;i++){
            if (array[i] % 10 !=7; && array[i]/10%10 !=7 && array[i] % 2 == 0){
                sum += array[i];
            }
        }
        System.out.println(sum);
    }
    // 数组倒置
    int[] newArray = new int[array.length];
    for(int i = 0;i<array.length;i++){
        newArray[i] = array[array.length-1-i];
    }
    array = newArray;
    for(int i = 0;i<array.length;i++){
        System.out.println(array[i]);
    }
    int temp = 0;
    for(int i = 0;i < array.length;i++){
        temp = array[i];
        array[i]=array[array.length-i-1];
        array[array.length-1-i]=temp;
    }
}

二、面向对象

封装

可以将代码打包成一个更加形象的方法,方法是更高级的抽象。将方法封装成一个跟高级的对象。

//用代码描述一个车
public class car {
        //属性
        public String color;
        public int saddle;
        //车的方法
        public void run(){
            System.out.println("启动车");
            System.out.print;n("踩离合");
    public static void main(String[] args) {
		Car car = new Car();
        car.color="red";
        car.saddle=4;
        car.run();
        }
        
    }
}

基础数据类型和引用数据类型。除去基础类型,剩下的都是引用数据类型。只有引用类型可以new

基础数据类型不能new

String型特殊,可以不用new来定义,String也属于一个对象。但基础类型,如:int型不是。

 String a  = new String("af")

对象是属性和方法构成的

​ 栈 堆

引用(地址) 存放属性等(车/scanner)

​ 内存较大,执行效率较高

​ 方法

​ class

1.权限修饰符

	作用域						当前类					同包内				子孙类			其他类
	public						  √						√				      √				 √
	protected					  √						√				  	  √				 ×
	friendly					  √						√									 ×
	private						  √						×									 √

2.new的使用

(1).构造方法

1.不写返回值相关的东西。

2.构造方法必须和类名相同

class Dog(){
    /**
    * 1 new 其实是在调用构造方法
    * 2 如果一个类里面没有构造方法,会创建一个空的构造方法
    * 3 构造方法能够传参数,在构造期间就把对象的值赋好
    * 4 一旦有了参数的构造方法,空构造就不在了。
    */ 
	public Dog(){
		System.out.println("执行了构造方法");
	}
    public static void main(String args[]){
        // 调用一个构造方法;
        Dog dog = new Dog();
    }
}
(2).重载方法
	// 方法名相同
	// 方法的参数类型,参数个数不一样
	// 方法的返回类型可以不相同
	// 方法的修饰符也可以不相同
	// main方法也可以被重载
	public Dog(String color2,int age2){
        color = color2;
        age = age2;
    }
	public Dog(){
        
    }
(3).this关键字
	// this关键字调用本类中的属性,也就是本类中的成员变量
	// 可以通过this来调用本类中的其他方法
	// this调用本类中的其他构造方法,调用时放在构造方法的首行
	public Dog(String color,int age){
        this.color = color;
        this.age = age;
    }

3.下午练习

package com.xinzhi.people;
public class Dog {
    private String color;
    private int age;
    private String post;
    private String kind;
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPost() {
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public Dog(){
    }
    public Dog(String color,String post,String kind,String age){
        System.out.println("狗狗的颜色是"+color+",它今年已经"+age+"岁了,它游泳的标准姿势是"+post+",他的种类是"+kind);
    }
    private void teast(){
        System.out.println("小狗够被主人梳理了毛发");
    }
    public void meet(){
        teast();
        System.out.println("其他小狗狗看起来很羡慕它!");
    }
}
// ---------------------------------------------------------
package com.xinzhi.people;
public class boy {
    private int age;
    private String name;
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boy(){
    }
    public boy(int age,String name){
        this.age = age;
        this.name = name;
    }
    private void dressed(){
        System.out.println(this.name+"在家穿上了衣服");
    }
    public void run(){
        dressed();
        System.out.println("然后去公园跑步,他今年"+this.age+"岁了,跑的飞快!");
    }
}
// ---------------------------------------------------------
package com.xinzhi.people;
public class Car {
    private String color;
    private String number;
    private int seat;
    public Car(){
    }
    public Car(String color,String number,int seat){
        System.out.println("车的颜色是"+color+"它的车牌号是"+number+"车的座位是"+seat+"人座位");
    }
}
// ---------------------------------------------------------
// Main方法调用
package com.xinzhi.test;
import com.xinzhi.people.Car;
import com.xinzhi.people.Dog;
import com.xinzhi.people.boy;
public class test {
    public static void main(String[] args) {
        boy boy = new boy();
        boy.setAge(11);
        boy.setName("乐乐");

        Dog dog = new Dog("棕色","狗刨","牧羊犬","3");
        dog.meet();

        Car car = new Car("绿色","晋A32548",4);
    }
}
// ---------------------------------------------------------
package com.xinzhi.people;
import java.util.SortedMap;

public class Girl {
    private String ban;
    private int age;
    private String name;
    public String getBan() {
        return ban;
    }

    public void setBan(String ban) {
        this.ban = ban;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Girl(){
    }
    public Girl(String ban,int age,String name){
        run();
        System.out.println("我叫"+name);
    }
    private void run(){
        System.out.println("我跑了100米");
    }
}
// ---------------------------------------------------------
package com.xinzhi.people;
public class CowDung {
    private String smell;
    private String function;
    private String color;
    private int big;
    public void setFunction(String function){
        this.function = function;
    }
    private void flower(String color,int big){
        System.out.println("一朵鲜花插在了"+color+"色的长度大概在"+big+"厘米的牛粪上");
    }
    public CowDung(){};
    public CowDung(String color,int big){
        flower(color,big);
    }
    public void CdFction(){
        System.out.println("牛粪的功能就是"+function);
    }
}
// ---------------------------------------------------------
// Main方法对类的调用
package com.xinzhi.test;
import com.xinzhi.people.CowDung;
import com.xinzhi.people.Girl;
public class test2 {
    public static void main(String[] args) {
        Girl girl = new Girl("13",13,"xs");
        girl.setName("sdf");
        System.out.println(girl.getName());
        CowDung cd = new CowDung();
        cd.setFunction("被鲜花插");
        CowDung cowDung = new CowDung("黄",15);
        cd.CdFction();
    }
}

c class test2 {
public static void main(String[] args) {
Girl girl = new Girl(“13”,13,“xs”);
girl.setName(“sdf”);
System.out.println(girl.getName());
CowDung cd = new CowDung();
cd.setFunction(“被鲜花插”);
CowDung cowDung = new CowDung(“黄”,15);
cd.CdFction();
}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值