The third week of learning Java

Java third week of study is completed, write a blog about what they have learned and record income.

1. Object Oriented

Learning Content:

  1. Call the definition of class, create an object, access the properties, methods of: last week
  2. Overloaded methods: rules and use
  3. Class constructor: rules, class creation process
  4. Overloaded constructor: rules and use, multi-parameter configuration
  5. this keyword: ①this.xxxx; ②this (xx, xxx);
  6. Multi-parameter cascade access

Problems and the wrong question:

  1. Multi-parameter cascade visit:
    definition of the Worker class, its properties have an Address class, a staff of the property address and zip code
	public class Test {
	public static void main(String[] args) {
		
		Worker zhangsan = new Worker("zhangsan",25,2500,new Address());
		//多参级联构造,先暂时为worker的addr属性建一个新对象
		zhangsan.addr.address=("北京市海淀区清华园1号");//给worker对象的addr属性赋值
		zhangsan.addr.zipCode = ("100084");
		System.out.println(zhangsan.name 
				+ zhangsan.addr.address 				//从worker对象的addr属性取值
				+ zhangsan.addr.zipCode);	
	}
}
class Worker{					//Worker类
	String name ;
	int age;
	double salary;
	Address addr;				//addr属性为worker对象的地址和邮编
	public  Worker() {}
	public  Worker(String name , int age , double salary,Address addr) {
		this.name = name ;		//多参构造
		this.age = age;
		this.salary = salary;
		this.addr =addr;
	}
}
class Address{					//address类
	String address ;
	String zipCode ;
	public  Address() {}
	public  Address(String address , String zipCode) {
		this.address = address ;//address类的多参构造
		this.zipCode = zipCode ;
	}
}

2. The three characteristics of an object

Learning Content:

Package private:

Use: private and public property method

Inheritance extends:

1. The concept of inheritance, requirements, select the parent class
2. Three points can not be inherited: constructor, private property modification method, qualifier limit
3. Access modifiers rules: private, default, protected, public in this category, the same package, limit class buns extraordinary provisions
covering 4. the method of
using 5.super keyword: super.xxx; Super (xx, xxx);
6. inheritance in object creation: • create subclass object must be created parent object ·
7.this contrast and super keywords

Polymorphic:

And grammatical concepts
polymorphic usage scenarios:
1. The method of the parent class as the parameter, various subclasses of the parent class as an argument to a parameter, the function calls (cover) corresponding to subclasses Method
2. Parent as a return value class, the subclass qualified returned to use as the return value out, the method can be called at the call coverage subclass had
upward transition and the downward transition:
1. upcast: references to the parent class subclasses object, the object can only use the properties of the parent class
2. downcast: parent references subclass object, then forced back to their own subclass type
① grammar and cast abnormal
②instanceof keywords

Problems and the wrong question:

  1. A set of array Animal Dog class chosen to form a new array, and traverse it out:
public class Q12 {

	public static void main(String[] args) {

		Animal[] as = new Animal[] {
				new Dog("Pluto") ,
				new Cat("Tom"),
				new Dog("Snoopy") ,
				new Cat("Garfield")};
		//将所有狗类挑选出来,放在同一个数组
		Dog[] dogs = getAllDog(as);
		for(int i = 0 ; i<dogs.length; i++) {		//遍历所有狗
			System.out.println( dogs[i].getName() );
		}
	}
	

	//挑狗的函数:
	public static Dog[] getAllDog(Animal[] as) {	//多态场景一:父类形参
		
		int acount = 0;
		//1.计数器,用来确定创建的狗数组的长度
		for(int i = 0 ; i<as.length;i++) {
			if(as[i] instanceof Dog) {
				acount++;
			}
		}
		
		Dog[] dogs = new Dog[acount];	
		//2.依计数器创建狗数组
		int size = 0;	
		//3.有效长度确定数组的下标				
		
		//4.将狗挑出来放在狗数组
		for(int i = 0 ; i<as.length;i++) {
			if(as[i] instanceof Dog) {		//符合要求,狗下标填值
				dogs[size] = (Dog)as[i];	//这里使用到了强转,从Animal[]拆出来的元素默认为Animal,其真正类型为狗,从Animal强转回来
				size++;//有效长度增加
			}
		}
		return dogs;
	}
}


class Animal{
	private String name;		//封装的属性
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public Animal(){}
	public Animal(String str) {
		setName(str);
	}
}
class Dog extends Animal{
	public Dog() {}
	public Dog(String str) {
		super(str);
	}
	
}
class Cat extends Animal{
	public Cat() {}
	public Cat(String str) {
		super(str);
	}
}
  1. Inheritance and covered Method:
    graphic class, can be calculated perimeter and area of the graphics
    pattern classes subclass a circle, having a radius attribute
    pattern subclass second rectangular with length and width properties
    rectangular sub-class rectangle with a side length of attributes
import java.lang.Math;
public class Question15 {

	public static void main(String[] args) {
			Circle c1 = new Circle();
			c1.radius = 3.0;			//半径为3.0的圆
			System.out.println(c1.area() + "\n" + c1.girth());
			Square s1 = new Square();
			s1.setSide(5.0);			//边长为5.0的正方形
			System.out.println(s1.area() + "\n" + s1.girth());
	}
}

class Shape{					//祖父类:图形	
	public double area() {		//能计算面积并返回
		return 0;
	}
	public double girth() {		//能计算周长并返回
		return 0;
	}
}


class Circle extends Shape{		//二叔类:圆形
	double radius;						//半径
	public double area() {				//面积
		double s;						
		s = radius * radius *  Math.PI;	
		return s;
	}
	public double girth() {				//周长
		double c;
		c = 2 * Math.PI * radius;
		return c;
	}
}

class Rect extends Shape{		//父亲类:矩形
	double length1;					//长
	double width;					//宽
	public double area() {				//面积
		double s;						
		s = length1 * width ;	
		return s;
	}
	public double girth() {				//周长
		double c;
		c = length1 + width;
		return 2*c;
	}
}
class Square extends Rect{		//自己类:正方形
	private double side;				//私有边长
	public void setSide(double side) {	//set边长时一块set父类长宽
		this.side = side;
		this.length1 = side;
		this.width = side;
	}
	public double getSide() {		
		return side;
	}
										//之后继承父类面积、周长
}

3. Three modifiers

Learning Content:

Abstract abstract:
abstract class, an abstract method

Problems and the wrong question:

  1. The calculated pattern perimeter to area above functions and methods Abstract:
abstract class Shape{						//抽象——祖父类:图形	
	abstract public double area() ;			//抽象方法——计算面积
	abstract public double girth() ;		//抽象方法——计算周长
}
Released four original articles · won praise 0 · Views 125

Guess you like

Origin blog.csdn.net/Mboy_BeYourself/article/details/104444781