1. 程式人生 > >Java與python的面向物件對比

Java與python的面向物件對比

Java

Python

public class Employee {
    public static int empCount = 0;
    String name;
    int salary;
    public Employee(String name, int salary)
    {
        this.name = name;
        this.salary = salary;
    }
    public void displayCount()
    {
        System.out.print("Total Employee " + Employee.empCount);
    }
    public void displayEmployee()
    {
        System.out.print("Name : " + this.name +  ", Salary: " + this.salary);
    }
}

class Employee:

   '所有員工的基類'

   empCount = 0

   def __init__(self, name, salary):

      self.name = name

      self.salary = salary

      Employee.empCount += 1

   def displayCount(self):

     print "Total Employee %d" % Employee.empCount

   def

displayEmployee(self):

      print "Name : ", self.name", Salary: ", self.salary

empCount靜態變數

__init__構造器

selfthis