انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة
الكلية كلية تكنولوجيا المعلومات
القسم قسم البرامجيات
المرحلة 2
أستاذ المادة احمد مهدي محمد سعيد الصالح
31/01/2016 20:18:26
Constructor and Set and Get Methods: Constructor Used to initialize an object of the class and any members. Called only once for the lifetime of the object being initialized: public class Person { String person_name; // constructer public Person(String name) { person _name = name; } } Example usage: // Initialize object of Person class by calling constructor. Person p = new Person("College of IT");
Get and Set Methods Used to get or set values of object members respectively. Unlike constructors, set methods can be used to initialize member values more than once: public class Person { // Member. String person_name; // Constructor. public Person(String name) { person_name = name; }
// Get member value. public String getName() { return person_name; }
// Set member value to given value. public void setName(String name) { person_name = name; } }// end of class Example usage: // Initialize object of Person class. Person p = new Person("College of IT"); String name; // Get name of this person. name = p.getName(); System.out.println(name);
// Set new name for this person. p.setName("University of Babylon"); name = p.getName(); System.out.println(name);
Output: College of IT University of Babylon
To understand the effects of public and private, consider the following program: // Public vs private access. class MyClass { private int alpha; // private access public int beta; // public access int gamma; // default access (essentially public) /* Methods to access alpha. It is OK for a member of a class to access a private member of the same class. */ // the constructer public MyClass() { alpha=90; } void setAlpha(int a) { alpha = a; } int getAlpha() { return alpha; } }// end class MyClass
class AccessDemo { public static void main(String args[]) { MyClass ob = new MyClass();
/* Access to alpha is allowed only through its accessor methods. */ ob.setAlpha(-99); System.out.println("ob.alpha is " + ob.getAlpha()); // You cannot access alpha like this: // ob.alpha = 10; // Wrong! alpha is private! // These are OK because beta and gamma are public. ob.beta = 88; ob.gamma = 99; } }
Type Checking Java is a strongly types language, which means that the Java compiler checks the code to ensure that every assignment and every call is type correct. If a type error is discovered, compilation fails with an error message. Type checking depends on the fact that every variable declaration gives the type checking of the variable, and the header of every method and constructor defines its signature: the types of its arguments and results (and also the types of any exceptions it throws). This information allows the compiler to deduce an apparent type for any expression and this deduction then allows it to determine the legality of an assignment For example consider int y= 7; int z= 3; int x= num.gcd (z,y); // discuss that
المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
|