Java OOP

作者: FTVBeginning | 来源:发表于2016-04-29 11:20 被阅读0次

A class: is a set of instructions that describe how a data structure should behave.
A class constructor: creates instances. Every class has at least a constructor.
Object: is an instance of a class that stores the state of a class.
Instance variables: describe details of the object
Inheritance: is used to inherit or share behavior from another class. 'extends' keyword is used to indicate the inherit behavior

Example:
Animal.java
class Animal {

public void checkStatus() {

    System.out.println("Your pet is healthy and happy!");

}

}
Dog.java
class Dog extends Animal {

int age;

public Dog(int dogsAge) {

age = dogsAge;

}

public void bark(){
System.out.println("Woof!");
}

public void run(int feet){
System.out.println("Your dog ran" + feet + "feet!");
}

public int getAge(){
return age;}

public static void main(String[] args) {
        Dog spike = new Dog(2);
    spike.bark();
    spike.run(2);
    int spikeAge = spike.getAge();
    System.out.println(spikeAge);
    spike.checkStatus();

}

}

Data Structure:
ArrayList: stores a list of data
HashMap: stores keys and asscociated values like a dictionary

Example:
import java.util.*;

public class GeneralizationsD {
public static void main(String[] args) {
ArrayList<String> sports = new ArrayList<String>();
sports.add("Football");
sports.add("Boxing");

    for(String sport : sports) {
        System.out.println(sport);
    }

    //Major cities and the year they were founded
    HashMap<String, Integer> majorCities = new HashMap<String, Integer>();

    majorCities.put("New York", 1624);
    majorCities.put("London", 43);
    majorCities.put("Mexico City", 1521);
    majorCities.put("Sao Paulo", 1554);


    for (String city: majorCities.keySet()) {

        System.out.println(city + " was founded in " + majorCities.get(city));

    }



}

}

相关文章

  • Java OOP

    A class: is a set of instructions that describe how a dat...

  • Hotspot Oop模型

    Java对象通过Oop来表示。Oop指的是 Ordinary Object Pointer(普通对象指针)。在 J...

  • Java中的OOP

    一.OOP Java是一个面向对象程序设计(Object Oriented Programming,OOP)的语言...

  • C++OOP对象的内存布局

    HotSpot采用了OOP-Klass模型来描述Java类和对象。OOP(Ordinary Object Poin...

  • Hibernate 入门1

    Related cst.zju Java-OOP course week 2 connect java code ...

  • Fundamentals of OOP and Data Str

    下载地址:Fundamentals of OOP and Data Structures in Java[www....

  • Android面试

    JAVA 基础 java的特点 (OOP),封装、继承、多态 ==、equals、hashCode的作用,区别 什...

  • 【知识详解】JAVA基础(秋招总结)

    JAVA基础 目录 JAVA基础 问:面向过程(POP)和面向对象(OOP)? 问:Python和Java的区别?...

  • JDK中的设计模式

    oop Examples of GoF Design Patterns in Java's core libra...

  • delete OOP JAVA

    决定quit这门课,理由如下: 更多是对各种包的使用,不够JAVA 问题的解决很无奈,让人心急而无用 心态有点爆炸...

网友评论

      本文标题:Java OOP

      本文链接:https://www.haomeiwen.com/subject/leshrttx.html