var a = new Base(); a.bar(); //outputs "Hello" to the console var b = new Child(); b.bar(); //outputs "World" to the console //The Base instance that is the prototype of b has the bar method changed above
Child.prototype = (function() { var Base = function() {}; Base.prototype = Parent.prototype; return new Base(); }());
function Base(){ this._priv = "Hello world" }; Base.prototype = { fn: function(){ console.log(this._priv); } } var obj2 = new Base(); obj2.fn = function(){ ... }
function Base() {} var base = new Base(); alert( base instanceof Base); // true Base.prototype = {}; alert( base instanceof Base); // false alert( base instanceof Object); // true
function Base(){} Base.prototype.foo = function() { alert("base"); }; function Derived() {} //Derived.prototype = new Base(); //not ideal - see the comments Derived.prototype = Object.create(Base.prototype); //grab MDN shim for older IE Derived.prototype.constructor = Derived; Derived.prototype.foo = function() { alert("overridden"); }; var b = new Base(); var d = new Derived(); b.foo(); d.foo();
var obj = new Base; obj instanceof Base; // true Derived.prototype = Base.prototype; Base.prototype = {}; // something else alert(obj instanceof Derived); // true alert(obj instanceof Base); // false
List<Derived> x = new ArrayList<Derived>(); // This is valid... List<? extends Base> y = x; // This is what you *want* to be valid List<Base> z = y; z.add(new Base()); // This looks fine at compile-time... but what would you expect it to do? Derived d = x.get(0);
void addItem(List<Base> list) { list.add(new Base()); } List<Base> l1 = new ArrayList<Base>(); addItem(l1); Base b = l1.get(0); List<Derived> l2 = new ArrayList<Derived>(); addItem(l2); Derived d = l2.get(0); // Would cause runtime exception.
Base base = new Base(); base.a="a"; base.b="b"; Gson gson = new Gson(); JsonElement je = gson.toJsonTree(base); JsonObject jo = new JsonObject(); jo.add(Base.class.getName(), je); System.out.println(jo.toString());
//Uses myMethod but keeps it hidden public class HiddenBase { private final Base base = new Base(); private void myMethod(); public void otherMethod() {base.otherMethod();} }
public static void main(String[] args) throws Exception { String sequence = "ABCAABBBABACCACC"; Base aBase = new Base(); for (int i = 0; i < sequence.length(); i++) { char temp = sequence.charAt(i); aBase.getMethod(temp).invoke(aBase); } }
/** * Initialize a coordinate base. */ private void initBase(double[] lowerBound, double[] upperBound, boolean extendBound) { base = new Base(lowerBound, upperBound, extendBound); backupBase = base; baseGrid = new BaseGrid(base); }
Base b; if (some condition) { b = new Base(); } else { b = new Derived(); } b.ggg(); // Ok, or not?
/** * Initialize a coordinate base. */ private void initBase(double[] lowerBound, double[] upperBound, String[] axisLabels) { base = new Base(lowerBound, upperBound); backupBase = base; baseGrid = new BaseGrid(base, axisLabels); }
public static void main(String[] args) throws Exception { Derived derived = (Derived) getObject(); } public static Base getObject() { if (Math.random() > 0.5) { return new Base(); } else { return new Derived(); } }
/** * Initialize a coordinate base. */ private void initBase(double[] lowerBound, double[] upperBound) { base = new Base(lowerBound, upperBound); backupBase = base; baseGrid = new BaseGrid(base); }
public class SubBase extends Base{ public void someMethod(){ //here i need the Base data Base b = new Base(); String name = b.getName(); String address = b.getAddress(); } }
class AssertionCheck { public static void main(String []args) { Base base = new Base(); try { base.foo(); } catch(Exception e) { base = new Derived(); base.foo(); } } }
Base base = new Base(); Base.Nested baseNested = base.new Nested(); Derived derived = new Derived(); Derived.Nested derivedNested = derived.new Nested(); Base.Nested d = derivedNested;
Base obj = new Sub(); obj.fun(); //-----------> In fun obj.display(); //-----------> Error obj = new Base(); obj.fun(); //-----------> fun