java object-oriented practice 1

/*

  • java object-oriented
  • Three main lines
  • 1, members of the class and the class java
  • Members of the class: properties, methods, constructors, code blocks, inner class
  • 2, three characteristics of object-oriented
  • Encapsulation (Encapsulation), polymorphism (Polymorphism), inherited (Inheritance), (abstract abstractness)
  • 3. Use other keywords
  • this,super,static,final,abstract,interface,package,import
  • Etc.
    * /

/*

  • Object Oriented (OOP Object Oriented Programming) and the process-oriented (POP Procedure Oriented Programming) Comparative
  • 1, both a thought, is relative to the object-oriented process-oriented, process-oriented, emphasizing the functional behavior in order to function as the smallest unit, consider how to do it.
  • Object-oriented, functional encapsulated into objects, have emphasized the object function to the class / object as a minimum unit, considered by whom.
  • 2, more emphasis on the use of object-oriented logical thinking human thinking routine methods and principles used, e.g. abstract, classification, inheritance, polymerization, polymorphic
  • For example child: to achieve "who put the fruit into the refrigerator."
  • A process-oriented:
  • 1, open the refrigerator door
  • 2, pick up fruit in the refrigerator
  • 3, close the refrigerator door
  • Second, the object-oriented
  • 1, target: human - Step 1: Open (Object 1: refrigerator) door: Step 2: Pick up (Object 2: fruits), Step 3: Close (Object 1: refrigerator) door
  • 2, the object 1: Refrigerator - Function 1: to open, function 2: Block
  • 3, the object 2: Fruit - Function 1: can be picked up
  • After instantiating the object, call the appropriate function related objects designed to achieve.
  • Object-oriented design
  • A programmer by the executor of process-oriented object-oriented commander turned
  • Second, the object-oriented analysis of the problem steps:
  • 1, according to the issues to choose real-world entities for the issue.
  • 2, from the entity seeking to solve problems related to the properties and functions of these attributes and functions evolved into the concept of world class
  • 3, the abstract entities described in a computer language, the computer world is formed in the definition of classes, i.e., by some kind of programming language,
  • The computer can be configured to identify the type and structure of data processing
  • 4, the class instance into the computer world object, the object is the ultimate tool for computer-world problem-solving
  • example
  • The amount of the ticket conductor statistics
  • Conductor and tickets can be a class
  • Statistical method conductor of the action is, where the ticket is the amount of data
  • General terms can create classes, methods verb class
    * /

/*

  • Object-oriented two elements:
  • Class (class) and the object (Object) is the core object-oriented concepts, all things are all objects
  • Class 1 is a description of a class of things, is to define the abstract concept
  • 2 is an individual objects such things actually exist, which is also known instances (Instance).
  • Abstract = class people, a specific individual target =
  • Object-oriented programming focused on the design of the class
  • The design is the design class class members, depending on the object that has the function of class design

*/

/*

  • World java code from a variety of different functions of classes
  • Class members in common:
  • 1, Properties Field (translated domain or field) = class member variable corresponding to
  • 2, the behavior of the method (function) corresponding to the class membership = Method (function)
  • Object class = class is created to instantiate instances of classes =
  • Corresponds to the real world, human beings have attributes such as age, height, there is talk of running and other functions
  • Syntax class
  • Modifiers class class name {// consider modifier (if needed), the class name (whether the same name, whether a keyword, whether clear image)
  • Property declaration; // (modifiers, property type, property name, initial value)
  • The method of statement; // (modifier, return type, method name, parameters, etc.)
  • }
  • Description: modifier can be expressed in any public access
  • Private modifier representation can be accessed only within the class
  • {} Class required body wrap
  • example
  • public class Person1{
  • private int age; // declare a private variable age
  • public void showAge (int i) {// declare a method showAge ()
  •  age = i;
    
  • }
  • }

*/

/*

  • The use of classes and objects (object-oriented implementation)
  • 1, create a class, members of the design class
  • 2, create a class of objects
  • 3, by calling the object "Object Property" or "object. Method" related functions
  • Note: If more than one object of a class created, each object has its own set of classes of property (non-static case)
  • In a separate memory space, the initialization value of the property with a value of initializing the same dimensional array
  • If you modify the properties of an object does not affect another property of the object
    * /

/*

  • Create and use objects: Memory resolved
  • A stack (Heap), the sole purpose of this memory area is stored object instance, almost all of the object instances to allocate memory here,
  • Described in the java virtual machine is: all object instances and arrays should be allocated on the heap
  • 2, typically of said stack (Stack), the stack is a virtual machine, a virtual machine for storing a stack of local variables, local variable table storage
  • Compiler known various basic data types (boolean, byte, char, short, long, float, double) the length,
  • Object reference (reference type, it is not identical with the object itself, but the first target address in the heap memory). After the implementation of the method, automatic release
  • 3, method area (Method Area), class information for storing the virtual machine has been loaded, a constant, static variables, the real-time data such as the code compiler

*/

package com.atguigu.contact;

public class Object1 {
	public static void main(String[] args) {//测试类中创建main方法作为入口
		Person people = new Person();//实例化Person类
		//调用属性格式 :对象.属性
		people.name = "john";
		people.age = 18;
		people.isMarried = false;
		people.gender = '男';//调用属性赋值
		//调用方法格式 :对象.方法
		people.eat();
		people.talk();
		people.sleep();
		people.work();
		System.out.println(people.display(1));//调用方法输出
		Person1 people1 = new Person1();
		//调用构造器格式 :对象.构造器(参数)
		people1.Person("Tom",30, false);//调用构造器赋值
		System.out.println(people1.display(2));//赋值后调用方法输出
	    //新创建一个对象,看属性的初始化值
		Person people2 = new Person();//方法内,局部变量
		System.out.println(people2.name);//null
		System.out.println(people2.age);//0
		System.out.println(people2.gender);//空值
		System.out.println(people2.isMarried);//false
		//
		Person people3 = new Person();
		people3 = people;
		people3.age = 10;
		System.out.println(people3.name);//输出john
		System.out.println(people.age);//输出10,与数组类似,直接赋值与复制不同
		//相当于将people 的地址赋值给people3,指向的是同一个内存空间。

				
}
}

class Person{
	String name;
	int age;
	char gender;
	boolean isMarried;//属性声明部分,成员变量
	public void eat() {//没有返回值需加 void
		System.out.println(name + "吃了一顿饭");
	}
	public void talk() {
		System.out.println(name + "唠了一会嗑");
	}
	public void sleep() {
		System.out.println(name + "睡了一会觉");
	}
	public void work() {
		System.out.println(name + "干了一会活");
	}
	public String display(int index) {//可以在括号里定义变量,在调用时赋值使用,形参,局部变量		
		return (index + "号人物名字是:" + name + "  年龄是:" + age + "  性别是:" + gender + "  是否已婚:" + isMarried);
	}//方法声明部分
}

class Person1{
	//属性
	String name;
	boolean isMarried;
	int age;
	//构造器
	public void Person() {}
	public void Person(String n,int num,boolean mark) {//形参,局部变量
		name = n;
		isMarried = mark;
		age = num;//构造器内部变量,局部变量
	
	}
	//方法
	public void walk () {
		System.out.println(name + "向前走了一段路");
	}
	public String display(int index) {
		return index + "号人物名字是:" + name  + "  年龄是:" + age + "  是否结婚" + isMarried;
	}
	//代码块
	{
		name = "hank";
		isMarried = true;
		age = 20;//代码块内,局部变量
	}
	//内部类
	class pet{
		String name;
		float weight;
	} 	
}
Published 47 original articles · won praise 1 · views 1082

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/103978890