Java基础班 第六天

代码部分:

第一部分:类的创建和应用  对应的第一节到第六节

①  这个创建的方法和数组有不一样的地方,数组是有两种创建方式;也可以分步创建

② 面对对象的概念,从执行者到指挥者。三大特征 封装 继承 多态  

③ 类中的方法和属性对应着真实生活中的事物的行为和事物的属性。类是一类有共同特征的集合,对象是类的具体表现

code1:自己student的拼写错误,然后创建的过程中,不能再student后面那个括号里面直接赋值,需要一个语句

class Demo1_Student 
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		//Student s = new Student("23","mao");
		//Studnet s = new Student();           这个完全是拼写错误
		//Student s = new Student("23","mao");   这样和数组一样写还是不行的
		Student s = new Student();
		s.name = "张三";
		s.age =23;
		System.out.println(s.name+s.age);
		s.shuijiao();
	}
}
class Student
{
	int age;
	String name;
	public void shuijiao(){
		System.out.println("Hello World!");
	}

}

code2:和上一个代码类似 注意,这个类的方法里没有static 这个词,调用方法的时候,是p20.call();有这个括号,不要忘了

class Demo2_phone 
{
	public static void main(String[] args) 
	{
		
		phone p20;
		p20 = new phone();
		p20.brand  = "apple";
		p20.price = 20000;
		System.out.println(p20.brand+p20.price);
		p20.call();
		p20.photo();
	}
}
class phone
{
	String brand;
	int price;
	public void call(){
		System.out.println("hi");
	}
	public void photo(){
		System.out.println("wave leg");
	}
}

第二部分:面向对象内存图  

这部分对应的第7节到11节

首先是有几个内存图,和数组那里类似 这个忘了 

② 成员变量和局部变量的区别

③基本数据类型:                   引用数据类型:数组、类、接口、枚举

code1:这个也是类的创建  自己编写错误是在car类里的方法,run方法,中,输出语句里,是直接color,不是car.color,这个要注意

class Demo1_Car 
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		car c;
		//c=new car;
		c = new car();
		c.color = "black";
		c.wheels = 4;
		c.run();
		
	}
}
class car
{
	String color;
	int wheels;
	public void run(){
		//System.out.println(car.color);
	System.out.println("Hello World!");
	System.out.println(color+wheels);
	}
}

code2 person类  这次写对了 不要着急  具体类的引用,不用加其他 

class Demo2_Person
{
	public static void main(String[] args) 
	{
		System.out.println("Hello World!");
		person p;
		p = new person();
		p.name = "er";
		p.age =22;
		p.fangfa();
	}
}
class person
{
	int age;
	String name;
	public void fangfa(){
	int num = 10;
	System.out.println(num);
	System.out.println(name);
	}
}

第三部分:形式参数是引用数据类型  

这个里面自己仔细回味一下,这个里面还有方法重载的问题,还有一个错误是 自己再写数字的那个print方法适合,不知道里面应该写什么东西。这个形式参数和实际参数的概念自己要搞明白,写了一个方法,这个方法的形式参数是自己写的一个类,这个原理和int string什么本质是一样的,所以你传入的必须也是这个类。

class  Demo1_student
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		student s;
		s = new student();
		print(s);
		
		print(20);
	}
	public static void print(int a){
		//print(a);
		System.out.println(a);
	}
	public static void print(student a){
		a.age = 23;
		a.name = "张予曦";
		a.speak();
	}
}
class student
{
	int age;
	String name;
	public void speak(){
		System.out.println(age+name);
	}
}

第四部分 匿名类  

① 首先是匿名对象的使用 这个就看代码吧

② 主类里的方法抽取,怎么看怎么别扭,怎么这个方法里还带传值得,不应该定义的时候,自己赋值吗??但自己还是需要自己理解

③充分理解自己的class类和Int类是平级的,自己要弄清楚这个问题。

code 1 :这个里面涉及了一个匿名对象不能赋值,只能调用方法: 
    

class  Demo2_car
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		//new car();
		//new car().color = "black";
		//new car().wheels = 4;
		new car().run();
	}
}
class car
{
	String color;
	int wheels;
	public void run(){
		System.out.println(color+wheels);
	}
}

那个new car.color那个纯粹没有意义,不能赋值 自己试过了

code2 这个里面有很多东西 自己要好好体会 自己在主类的方法里写了一个方法,这个方法里 自己不知道该写什么,一开始写错误了,以为是没有加数据类型,后来加了数据类型还是错,后来才想到,应该用上形式参数,然后才对了,这里抽取了个什么方法,真的看不太懂

class  Demo3_car
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		fangfa(new car());
	}
	public static void fangfa(car c){
		//color="black";
		//wheels = 4;
		//Sring color = "black";
		//String color = "black";
		//int wheels = 4;
		c.color = "black";
		c.wheels = 4;
		c.run();
	}
}
class car
{
	int wheels;
	String color;
	public void run(){
		System.out.println(color+wheels);
	}
}

第五部分:封装  

这部分讲解的就是对自己写的类进行私密化操作,就是封装,然后这里有个错误,就是p1.getage这种写法是错误的,必须加上一个括号,方法的调用必须加括号,不加括号那是属性

class Demo1_person
{
	public static void main(String[] args) 
	{
		
		person p;
		p =new person();
		p.name = "迪丽热巴";
		p.setage(17);
		System.out.println(p.getage()+p.name);
	}
}
class person
{
	String name;
	private int age;
	public void setage(int a){
		age = a;
	}
	public int getage(){
		return age;
	}
	public void run(){
		System.out.println(name+age);
		}
}

下一个代码:这个引入了一个关键字 this 用来区分局部变量和成员变量 

class  Demo1_this
{
	public static void main(String[] args) 
	{
		
		person p;
		p =new person();
		p.setage(17);
		p.setname("迪丽热巴");
		System.out.println(p.getname()+p.getage());
	}
}
class person
{
	private int age;
	private String name;
	public void setage( int age){
		if(age<=0|age>=200){
			System.out.println("Hello ET");
		}else{
			this.age =age;
		}
	}
	public int getage(){
		return age;
	}
	public void setname(String name){
		this.name = name;
	}
	public String getname(){
		return name;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_40079205/article/details/82929975
今日推荐